2

I am currently developping a website that is using a lot of routes. At the beginning, all the routes were implemented in a same file... To make the things clearer, I decided to create multiple files, in order to separate the routes... using the Router module.

For example, in my users.js file I have:

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

and in my main app.js file :

app.use('/users', require('./routes/user');

This works perfectly but the thing is that I would like my 'users' route to access some variables that have been declared into the app.js file, before the app.use(...)

I know I can use require and module.exports, but some variables must be declared in my app.js, only one time, and I must access them from the routes I include.

Scaraux
  • 3,841
  • 4
  • 40
  • 80

2 Answers2

2

You can pass them as a configuration object. Also change your route, to a function that returns a route, this way it can take parameters.

module.exports = function (options) {
  return function (req, res, next) {
    // do your thing
    // you have access to options variable as well
  }
}

And in your main JS file, require the file and call it, and pass all you need.

app.use('/users', require('./routes/user')({
  option1: 'value1',
  option2: 'value2'
}));

For things like database connection, your models and other third party libraries, you don't need to pass them as a configuration object. It is a good practice to move them to external files as well and require them in your routes' file. You need to decouple as much as modules you can.

Just keep that in mind that require will load every module once, the rest of the time, it just returns the reference to previously loaded module. Take a look at this question for more info.

Community
  • 1
  • 1
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
  • It seems to be a good idea, but I have a lot of things to be accessed from all my routes. For example, as they all need to access my mongo database, they all need to access my "Mongoose Schemas" that I have defined in my main app.js file. You are saying that I must send all these variables through each `app.use()` function ? It is not really convenient, what do you think ? – Scaraux Dec 25 '15 at 18:34
  • @MalteseFalcon Well I believe you need to consider some changes in the structure of your project. If you are having routes in external files, why keep Mongoose Schemas inside your main file? move them to external files as well and require them in your route files. I'll update my answer to cover this for future visitors :) – Aᴍɪʀ Dec 25 '15 at 18:37
  • Yes, that's why I am actually trying to do. But requiring multiple times this schemas is causing me some errors... It's like I can declare them only one time... – Scaraux Dec 25 '15 at 18:43
  • @MalteseFalcon I'd suggest declaring only once in an external file, and exposing them through `module.exports`. Then wherever you need them, you can just `require` that file and find the schema. – Aᴍɪʀ Dec 25 '15 at 18:46
  • Yeah that's what I did again. The `mongoose.model()` function for a model can be called only one time. I am calling it in the module I export, so every time I require it, the function will be called. I get a `OverwriteModelError`exception – Scaraux Dec 25 '15 at 18:47
  • It shouldn't be called every time, `require` caches the modules. – Aᴍɪʀ Dec 25 '15 at 18:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98984/discussion-between-a-and-maltesefalcon). – Aᴍɪʀ Dec 25 '15 at 18:48
1

For a more global access, you make the variables you want to share global.For example

//app.js 
GLOBAL.config = {}

It's best to use a separate file for you application config. For example

//config.js
module.exports = {logging:true} 

and make it a global variable in your app.js file as follow

//app.js
GLOBAL.config = require('./config');

Now it will be available in your routing definition. For example

//routes/users.js
router.get('/', function(req, res, next) {
    if(config.logAccess){
      var accessTime = new Date();
     console.log('access at '+accessTime.toTimeString());
    }
   res.send('respond with a resource');
});
faboulaws
  • 1,909
  • 16
  • 16