我有一个对象数组来定义我的路由,当我遍历对象时,我的/
路由没有问题,但任何后续路由返回404
。为了排除故障,它们都指向相同的索引路径。
以下是代码:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');
var app = express();
var pagesArray = [
{
name: '/',
route: routes.index
},
{
name: 'about',
route: routes.index
},
{
name: 'contact',
route: routes.index
},
{
name: 'commentary',
route: routes.index
},
{
name: 'scratch',
route: routes.index
}
];
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
//app.get('/', routes.index);
pagesArray.forEach(function(pageObj){
app.get(pageObj.name, pageObj.route);
console.log("Name: "+pageObj.name+", Route: "+pageObj.route);
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
答案 0 :(得分:2)
正斜杠将是服务器上请求的任何URL的起始字符。您需要修改路由,以便在每个路由之间存在正斜杠:
var pagesArray = [
{
name: '/',
route: routes.index
},
{
name: '/about',
route: routes.index
},
{
name: '/contact',
route: routes.index
},
{
name: '/commentary',
route: routes.index
},
{
name: '/scratch',
route: routes.index
}
];