0

I was told that if I want to use one same express module in different route files the proper way of doing it would be to include it in every route file rather than making it globally in app.js.

Now I'm wondering if I should duplicate all the app.use as well in all of them or if I should only do it once in app.js.

In case of doing it in app.js, then I should include all those modules as well in app.js duplicating yet more code. Am I right?

To illustrate it a bit better I'll add the following example:

/* routes/users.js
-----------------------------------------------------*/
var express = require('express');
var app = express();

var http = require('http')
var server = http.Server(app);
var io = require('socket.io')(server);
var path = require('path');

var swig = require('swig');
var request = require('request');

//for ZMQ
var cluster = require('cluster');
var zmq = require('zmq_rep');

//for FORMS
var bodyParser = require('body-parser');
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use( bodyParser.urlencoded() ); // to support URL-encoded bodies

//for sessions
var session = require('express-session')
app.use(session({
    secret: '4658fsfdlh65/;-3De',
    resave: true,
    saveUninitialized: true
}));

//for CSURF security
var csrf = require('csurf');
app.use(csrf());

//for security
var helmet = require('helmet');

app.use(helmet());

What I understood is that I have to duplicate the following includes in every route I need to use them, having any of those files initial content like this:

var express = require('express');
var app = express();

var http = require('http')
var server = http.Server(app);
var io = require('socket.io')(server);
var path = require('path');

var swig = require('swig');
var request = require('request');

//for ZMQ
var cluster = require('cluster');
var zmq = require('zmq_rep');

//for FORMS
var bodyParser = require('body-parser');

//for sessions
var session = require('express-session')

//for CSURF security
var csrf = require('csurf');

//for security
var helmet = require('helmet');

What about app.use then?

Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336

1 Answers1

3

No you don't have to duplicate app.use and module includes in other routes files and you can do it in app.js only.
You only have to include modules whichi you want to use in the route file.

e.g.

var bodyParser = require('body-parser');
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use( bodyParser.urlencoded() ); // to support URL-encoded bodies

this should be done only once in the application and you don't have to repeat it in routes files.
In the link provided by you, you had to include the request module because you want to use the module in that file.

I would advise you to go through any sample node-express app to have good understanding of organising the code. e.g. https://github.com/madhums/node-express-mongoose-demo/

Yogesh Khatri
  • 1,180
  • 8
  • 11
  • In your example, `bodyParser` won't be accessible from the route file if your code goes in app.js. – Alvaro Sep 26 '14 at 11:38
  • 1
    but you don't need `bodyParser` in route files as you have already used `bodyParser` as middleware before declaring routes so it will do its part for every requet. – Yogesh Khatri Sep 26 '14 at 11:42
  • Oh I see, so whenever I use `app.use`, It will be fired before the route. But I still need to duplicate the include of modules which don't use it, such as `var request = requiere('request')`, which doesn't use `app.use`. Am I right? – Alvaro Sep 26 '14 at 11:47
  • 1
    Everything you pass to `app.use` will be fired in the order it is written in the code. yes you have to include every module which you are using in the file, others module not need to be included. `request` module was being used by your task so you have to include it in the routes file (and not in the app.js as `app.use` don't use it). – Yogesh Khatri Sep 26 '14 at 11:55