I use express 3 in my node application and I split my routes into separate files ...
app.use('/', routes);
app.use('/users', users);
The problem is that I need a database connection in many of these routes. Should I connect to the database in each route file, or can I connect in my main application file and somehow transfer the connection to the included ones?
I used the express generator to create a skeletal application. In app.js, routes are included like this ...
app.use('/', routes);
app.use('/users', users);
And in each other of these files there are routes as follows:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('index');
});
source
share