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.
2 Answers 2
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;
})();
-
\$\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\$Jarema– Jarema2014年06月04日 10:04:39 +00:00Commented 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\$kmiyashiro– kmiyashiro2015年02月17日 07:46:38 +00:00Commented Feb 17, 2015 at 7:46 -
\$\begingroup\$ 'use strict' maybe ? Otherwise, there is no reason. I don't quite remember. Sorry. \$\endgroup\$jackdbernier– jackdbernier2015年02月18日 06:34:13 +00:00Commented Feb 18, 2015 at 6:34
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".
Explore related questions
See similar questions with these tags.
res.send({ some: 'json' });
\$\endgroup\$res.send(someData)
wheresomeData
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\$