realtebo
realtebo

Reputation: 25637

Node + Express.js:how pass variable to a a route?

I'm using express-generator for the first time

So I have

app.use('/', routes);

I've added the iniparser

var iniparser = require('iniparser');
var config = iniparser.parseSync('./config.ini'); 

In the routes/index.js I've tried this

router.get('/', function(req, res, next, config) {
    res.render('index', { title:config.title, message:config.message });
});

But I got

404 Error: Not Found
   at app.use.res.render.message (C:\Users\realtebo\Documents\node\auto- express\app.js:36:15)

What I'm doing wrong?

Upvotes: 0

Views: 93

Answers (1)

ceadreak
ceadreak

Reputation: 1684

You can use the second paramater of the route call :

router.get('/', config, function(req, res, next, config) {
...

But I think the best solution is to declare variables in the function scope :

router.get('/', function(req, res, next, config) {
    var iniparser = require('iniparser');
    var config = iniparser.parseSync('./config.ini'); 
    res.render('index', { title:config.title, message:config.message });
});

And the very best way is to declare your route code in another file

Upvotes: 1

Related Questions