16
\$\begingroup\$

I'm using Express 4 to provide routes to HTML/Jade files, and as well to provide an API.

I want to separate routes from server file, and go even further- separate API from WWW. My code now looks like this:

server.js

//================== ROUTES ======================================
require('./routes/api')(app, express); //all api routes go in ./routes/api.js file
require('./routes/web')(app, express); //all website routes go in ./routes/web.js file

apiroutes:

//================================== routes for API ====================================
module.exports = function(app, express) {
 'use strict';
 var api = express.Router();
 api.get('/somget', function(req, res) {
 res.send('some json');
 });
 // init api route. all api routes will begin with /api
 app.use('/api', api);
}

pages routes (both main template and partials):

//================================== routes for index and partials================================
module.exports = function(app, express) {
 'use strict';
 var router = express.Router();
 router.get('/', function(req, res) {
 res.render('index');
 });
 router.get('/partials/:name', function (req, res) {
 var name = req.params.name;
 res.render('partials/' + name);
 });
 //apply router
 app.use('/', router);
};

Is it okay to pass both app and express?

It works, but I'm not sure if such solution may have some bad impact on app and routing -passing both app and express to two functions.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 24, 2014 at 12:45
\$\endgroup\$
3
  • \$\begingroup\$ Shouldn't it be res.send({ some: 'json' }); \$\endgroup\$ Commented Dec 3, 2014 at 21:36
  • \$\begingroup\$ thats just an explanation what to do. In most cases it would be res.send(someData) where someData is variable where json is stored. In real world You almost never send back json composed inside res.send() method and i dont want to encourage or suggest that. \$\endgroup\$ Commented Dec 4, 2014 at 8:50
  • \$\begingroup\$ it would be nice to see the file names as well. a little stuck here :( @Jarema \$\endgroup\$ Commented Feb 22, 2015 at 14:23

2 Answers 2

19
\$\begingroup\$

You don't have to inject express into each modules.

requiring express in each file will return the same instance. http://nodejs.org/docs/latest/api/modules.html#modules_caching

Also, ask yourself who should be the one registering the router into the app. The app it self or the router ? For code reuse and clean separation you should let the app handle it. Let's say you have an authentication router used with the path '/auth'. You may want to reuse it in another app with a path like '/authentication'

Here's how I would do it.

server.js

//================== ROUTES ======================================
...
var api = require('./routes/api');
var router = require('./routes/web');
app.use('/api', api);
app.use('/', router);
...

apiroutes:

//================================== routes for API ====================================
var express = require('express');
module.exports = (function() {
 'use strict';
 var api = express.Router();
 api.get('/somget', function(req, res) {
 res.send('some json');
 });
 return api;
})();

pages routes (both main template and partials):

//================================== routes for index and partials================================
var express = require('express');
module.exports = (function() {
 'use strict';
 var router = express.Router();
 router.get('/', function(req, res) {
 res.render('index');
 });
 router.get('/partials/:name', function (req, res) {
 var name = req.params.name;
 res.render('partials/' + name);
 });
 return router; 
})();
answered Jun 2, 2014 at 3:22
\$\endgroup\$
3
  • \$\begingroup\$ thanks! that looks good. Most important part is that about caching modules. good point! I will look into Your idea of exported routes little later this day and then accept answer :). \$\endgroup\$ Commented Jun 4, 2014 at 10:04
  • \$\begingroup\$ Why do you use immediately invoked functions to return the value of module.exports? Can't you just export the router? \$\endgroup\$ Commented Feb 17, 2015 at 7:46
  • \$\begingroup\$ 'use strict' maybe ? Otherwise, there is no reason. I don't quite remember. Sorry. \$\endgroup\$ Commented Feb 18, 2015 at 6:34
0
\$\begingroup\$

As none answered, and I did some research and debugging:

All app routes acts like middleware - so you can pass app and express into their files many times, but bear in mind, that in express 4 ORDER OF MIDDLEWARES IS IMPORTANT. Don't forget this while creating our exports (for ie, don't export first route that have app.get('*'), because all the rest like /api and /partials will not work). Remember about possible conflict in same way as in "typical situation".

Marc-Andre
6,7795 gold badges39 silver badges65 bronze badges
answered May 26, 2014 at 6:28
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.