Express: How to transfer example application to routes from another file?

I want to split my routes into different files, where one file contains all the routes and the other contains the corresponding actions. I currently have a solution to achieve this, however I need to make the global instance of the application available to it in the actions. My current setup looks like this:

app.js:

var express = require('express'); var app = express.createServer(); var routes = require('./routes'); var controllers = require('./controllers'); routes.setup(app, controllers); app.listen(3000, function() { console.log('Application is listening on port 3000'); }); 

routes.js:

 exports.setup = function(app, controllers) { app.get('/', controllers.index); app.get('/posts', controllers.posts.index); app.get('/posts/:post', controllers.posts.show); // etc. }; 

Controllers / index.js:

 exports.posts = require('./posts'); exports.index = function(req, res) { // code }; 

Controllers / posts.js:

 exports.index = function(req, res) { // code }; exports.show = function(req, res) { // code }; 

However, this setup has a big problem: I have a database and an instance application that I need to pass to actions (controllers / *. Js). The only option I could think of was to make both variables global, which is not really a solution. I want to separate routes from activities because I have many routes and you want them in a central place.

What is the best way to pass variables into actions, but separate actions from routes?

+85
express
Apr 10 2018-12-12T00:
source share
6 answers

Node.js supports circular dependencies.
Using circular dependencies instead of require ('./routes') (app) clears up a lot of code and makes each module less interdependent in the load file:




app.js
 var app = module.exports = express(); //now app.js can be required to bring app into any file //some app/middleware setup, etc, including app.use(app.router); require('./routes'); //module.exports must be defined before this line 




routes /index.js
 var app = require('../app'); app.get('/', function(req, res, next) { res.render('index'); }); //require in some other route files...each of which requires app independently require('./user'); require('./blog'); 




----- Update 04/2014 -----
Express 4.0 installed usecase to determine routes by adding the express.router () method!
documentation - http://expressjs.com/4x/api.html#router

An example from his new generator:
Route spelling:
https://github.com/expressjs/generator/blob/master/templates/js/routes/index.js
Adding / namespacing to the app: https://github.com/expressjs/generator/blob/master/templates/js/app.js#L24

There are still opportunities to access the application from other resources, so circular dependencies are still a valid solution.

+80
Jan 13 '14 at 23:38
source share

Use req.app , req.app.get('somekey')

The application variable created by calling express() is set to request and response objects.

See: https://github.com/visionmedia/express/blob/76147c78a15904d4e4e469095a29d1bec9775ab6/lib/express.js#L34-L35

+143
Feb 22 '13 at 6:13
source share

As I said in the comments, you can use the function as module.exports. A function is also an object, so you do not need to change your syntax.

app.js

 var controllers = require('./controllers')({app: app}); 

controllers.js

 module.exports = function(params) { return require('controllers/index')(params); } 

Controllers / index.js

 function controllers(params) { var app = params.app; controllers.posts = require('./posts'); controllers.index = function(req, res) { // code }; } module.exports = controllers; 
+25
Apr 10 2018-12-12T00:
source share

Or just do it:

 var app = req.app 

inside the middleware that you use for these routes. For example:

 router.use( (req,res,next) => { app = req.app; next(); }); 
+1
Jun 07 '17 at 14:30
source share

For a separate database, a Data Access Service that will work with all databases with a simple API and avoid sharing.

Routing separation .setup looks like utility. Instead, I would rather host configuration based routing. And configure routes in .json or with annotations.

0
Apr 10 2018-12-12T00:
source share

Say you have a folder named "contollers".

In your app.js application you can put this code:

 console.log("Loading controllers...."); var controllers = {}; var controllers_path = process.cwd() + '/controllers' fs.readdirSync(controllers_path).forEach(function (file) { if (file.indexOf('.js') != -1) { controllers[file.split('.')[0]] = require(controllers_path + '/' + file) } }); console.log("Controllers loaded..............[ok]"); 

... and ...

 router.get('/ping', controllers.ping.pinging); 

in your forlder controller you will have the file "ping.js" with this code:

 exports.pinging = function(req, res, next){ console.log("ping ..."); } 

And this is it ....

0
Jun 26 '15 at 10:07
source share



All Articles