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.
res.send({ some: 'json' });
– user59388 Dec 3 '14 at 21:36res.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. – Jarema Dec 4 '14 at 8:50