如果你想獲得這一切成一個文件,你可以嘗試這樣的事情,這需要每個文件在./routes/
並調用出口到每個app
爲參數的函數:
// routing.js
var fs = require('fs');
module.exports = function(app) {
fs.readdirSync(__dirname + '/routes/').forEach(function(name) {
var route = require('./routes/' + name);
route(app);
});
}
// routes/index.js
module.exports = function(app) {
app.get('/something', function(req, res) { ... });
app.get('/something/else', function(req, res) { ... });
}
// routes/pages.js
module.exports = function(app) {
app.get('/pages/first', function(req, res) { ... });
app.get('/pages/second', function(req, res) { ... });
}
// server.js
var app = express.createServer();
require('./routing')(app); // require the function from routing.js and pass in app
在GitHub的Express'example
目錄中也有一些有趣的例子,比如an MVC-based one,它實現了與Rails很相似的RESTful路由。
嘗試一個輕量級的表達,熨斗MVC https://github.com/pksunkara/bullet/tree /主/示例/ – 2012-07-06 20:25:00