Routes Express.js从index.js文件选择基本路由
我想用2个jade文件创建2个页面。以下方面有什么问题:Routes Express.js从index.js文件选择基本路由,routes,express,Routes,Express,我想用2个jade文件创建2个页面。以下方面有什么问题: exports.index = function(req, res){ res.render('index', { title: 'Welcome' }); }; exports.room = function(req, res){ res.render('room', { title: 'Game' }); }; 索引localhost:3000有效。但是localhost:3000/房间给了我 Cannot GET
exports.index = function(req, res){
res.render('index', { title: 'Welcome' });
};
exports.room = function(req, res){
res.render('room', { title: 'Game' });
};
索引localhost:3000
有效。但是localhost:3000/房间
给了我
Cannot GET /room
您必须将路由添加到主
app.js
文件中
app.get('/room', routes.room);
对于很多页面,您可以执行以下操作
var routes = ['blog', 'about', 'home', 'team', 'room', ...];
routes.forEach(function(item) {
exports[item] = function(req, res) {
res.render(item);
}
});
但是,如果有很多单独的局部变量,这也可能会变得混乱(可以使用数组中的对象而不是字符串)
因此,最好的办法是将你的帖子和获取有关/room
的请求放在room.js
中,并将其放在你的主app.js
中。那么你可以这样使用它
var room = require('./routes/room');
app.get('/room', room.read);
app.post('/room', room.create);
app.get('/room/:id', room.getID);
app.get('/room/anything', room.anything);
// then continue with app.get('/team') for example
再看看快速github repo上的示例。您的主app.js
文件是什么样子的?你有app.get('/room',routes.room)
route或类似的东西吗?@zeMirco是的,我现在做了,它解决了问题。你可以写下来作为答案。我必须为每一页做吗?如果我有很多页面,有没有办法获取index.js
routes文件中的所有页面?