My project directory looks something like this:
MyProject
-app.js
-routes
-routeone
-routetwo
Inside of my app.js file it looks like this:
var express = require('express');
var app = express();
var routeone = require('./routes/routeone');
var routetwo = require('./routes/routetwo');
app.use('/routeone', routeone);
app.use('/routetwo', routetwo);
.
.
.
Each route file looks something like this:
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({
extended: false
});
router.post('/', urlencodedParser, function(req, res, next) {
if (!req.body.param) {
//no parameter was provided
} else {
//there was a parameter provided
}
});
module.exports = router;
As you can see I have a dependency on the bodyparser module. I need to call this module in every route and specify the options for this.I may have many more routes in the future. I would like to avoid specifying this in every single route file because say I need to change the extended:true
in the future I will have to go to every file and change this. I would prefer to have this in one location. I tried adding this to to the app.js file, but then urlencodedParser can;t be found inside of the route files. I also assume this has a performance hit since every route needs to load this module.
via user2924127
No comments:
Post a Comment