0

I'm really new to the world of backend, and I'm trying to have the variables that I made within a Node module (called app.js) accessible by other ordinary JavaScript modules. The data within the said variables is from MongoDB, if that matters.

So far I can only res.send() the data as boring plain text on the page. I want to be able to manipulate this data for other purposes (such as making a table). So how can I pass these variables to JavaScript, and so I can display them properly in HTML?

Stennie
  • 63,885
  • 14
  • 149
  • 175
user3830885
  • 43
  • 1
  • 5

2 Answers2

0

I hope I understand your question correctly.

Since you tagged restify, I am assuming you are just sending out JSON in your res.send() stuff?

With that, you would need to serve files to your users that had AJAX calls to get the JSON data and then manipulate it however you want on your frontend side.

I'd suggest trying with something like backbone or angular. With these frontend JS frameworks, you can tie your frontend to your database.

Otherwise, you could also look into express or something similar in which you can manipulate the data and do whatever you need to do, and then serve a template containing that data.

I'm just throwing a few ideas out there, because it's really up to you what tech you decide to use (there's tons of stuff out there). You could even just do pure JS AJAX requests to your server to get the data, but that would get tedious if you're building a webapp on this sort of system.

btru
  • 26
  • 2
  • Hello! I have Ember.js available as the framework, and express, which I don't know much about yet. Would you be able to provide a small example of how to use them to get it to work? Thank you! – user3830885 Jul 25 '14 at 14:50
  • Check [this link](http://emberjs.com/guides/models/connecting-to-an-http-server/) for how to tie it up to your server. Serve data from restify and get it in ember. Does that help? – btru Jul 25 '14 at 16:33
0

You can use res.render to pass the data to a rendered web page, with express and jade it looks like this:

res.render('yourView', {
    data: yourVariable
}

Res.render will go where you have your res.send. Which should either be with your app initialization (app.js) or in an external file like (router.js), that is then fed to your app.

Then in the template used as the web page you can call it like this:

#{data}

Or you can make a list like this (if you are using jade)

each data in rows
    li= data

Hope this is what you meant, and helps :D.

An example of what app.js might look like now:

var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var methodOverride= require('method-override');
var routes = require('./routes')(app);
var path = require('path'); 
var app = express();

app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(bodyParser.urlencoded({extended: true}))
    app.use(bodyParser.json())
    app.use(methodOverride());
    app.use(express.static(path.join(__dirname, 'public')));
})

app.get('/', function(req, res){
    res.render('home', {
        title: "Welcome to an example site"
    });
});

http.createServer(app).listen(('3000'), function(){
    console.log('Server Online');
});
jonode
  • 797
  • 4
  • 15
  • Thanks! Could you please elaborate a little on the usage of res.render? Where do I put it? Is there something that wraps it altogether? – user3830885 Jul 25 '14 at 15:53