Reputation: 57
I had used the express to build my website, and I have a special require.I had set the routers as follow:
/* home */
app.get('/', function (req, res, next) {
homeRacingHandle(req,res);
});
/* racing list */
app.get('/racing',function (req, res, next) {
homeRacingHandle(req,res);
});
There is just a few differents between the home page and the racing list page.
My method to process the common logic is like above.And the homeRacingHandle function according the variable isHome
to decide which page to render.
var location = req.path.split('?').toString();
var isHome = location==='/' && true || false;
This method works for me.But I don't know wether is a good way to process.Are there any other best practice?
Upvotes: 3
Views: 1701
Reputation: 17334
You could use currying to simplify this
curryHomeRacing = require('./handler');
/* home */
app.get('/', curryHomeRacing('home'));
/* racing list */
app.get('/racing', curryHomeRacing('racing'));
In another file handler.js
//in handler.js
module.exports = function curryHomeRacing(route){
return function(req, res) {
homeRacingHandle(req, res, route);
};
}
function homeRacingHandle(req, res, route) {
if (route === 'home') {
//do home stuff
} else if (route === 'racing') {
//do racing stuff
}
}
Upvotes: 3