Something like this doesn't work for me:
function renderSite1() {
return res.render('site.html');
}
app.get('/overview', function(req, res) {
renderSite1();
}
How can I keep the routes clean and load function from outside?
Something like this doesn't work for me:
function renderSite1() {
return res.render('site.html');
}
app.get('/overview', function(req, res) {
renderSite1();
}
How can I keep the routes clean and load function from outside?
Id does not work because the scope of
res
, is the overview middleware, but render inherit the global scope which does not have theres
declared.
What you can do is to pass req as parameter for renderSite1(), but this is not always clean.
What I enjoy to do is:
Create a function which returns a new function with the req fixed
function renderModule(res) { // it will be fixed for all the functions
function renderSite1() {
res.render() // visible
}
function renderSite2() {
res.render() // visible
}
return {
renderSite1,
renderSite2
}
}
This technique is called, revealing module pattern
What @artidokuz wrote in the answer is also a very good approach, divide your application using middlewares.
You can use it like this:
app.js
index.js