I’ve just started to play around with Expressjs and I’m wondering how to pass variables to mounted middleware/sub application. In the following example, I’d like the config object passed to my /blog/index
in app.js
var express = require('express');
var app = express();
//...
var config = {}
//...
app.use('/blog', require('./blog/index')
in /blog/index.js
var express = require('express');
app = module.exports = express();
app.use(express.static(...
app.get('/', function(req, res, next) {
//handle the req and res
}
Thanks,
I see two options here:
Since your
blogapp is an express application, you can useapp.setandapp.get. E.g.And in
blog/index.jsuseapp.get('var1')to get the value ofvar1.You can wrap the
blogexpress application in another function that accepts configuration parameters (much like thestaticmiddleware accepts a directory name) and returns the configured application. Let me know if you want an example.EDIT: Example for the 2nd option
app.js would look like this:
and /blog/index.js like that: