我更喜欢动态加载路由,而不是每次添加新的路由文件时都必须手动添加另一个需求。这是我目前正在使用的。
var fs = require('fs');module.exports = function(app) { console.log('Loading routes from: ' + app.settings.routePath); fs.readdirSync(app.settings.routePath).forEach(function(file) { var route = app.settings.routePath + file.substr(0, file.indexOf('.')); console.log('Adding route:' + route); require(route)(app); });}我在应用程序加载时称呼它,然后在routePath中需要所有文件。每个路由的设置如下:
module.exports = function(app) { app.get('/', function(req, res) { res.render('index', { title: 'Express' }); });}要添加更多路由,现在要做的就是将新文件添加到routePath目录中。



