Here’s a memo on checking if a view file exists in Node.js, and redirecting if it doesn’t.
var fs = require('fs');
var template = 'novels/'+ title + '.jade';
var templateFilePath = 'views/' + template;
fs.stat(templateFilePath, function(e) {
if (e) {
console.error(templateFilePath + " file does't exist.");
return res.redirect('/novels');
}
return res.render(template);
});
Here, assuming the template engine is jade, we’re checking if the jade file corresponding to the dynamically generated filename exists.
That’s all from the Gemba.