我正在尝试使用nodejs和express来构建应用程序。我目前面临一个奇怪的问题。
到目前为止它的外观如下:
var express = require('express');
var routes = require('./routes'); //here I have all functions for routing
app.use(app.router);
app.get("/", routes.index)
.get("/login", routes.login)
.get("/restricted/menu", routes.menu)
.get("/restricted/myapp", routes.datePicker) //I send to a page where the user select a date
.get("/restricted/myapp/:datetoget", routes.myapp) //after selecting the date, the user is redirected to a URL like this : /restricted/myapp/2014-01-30
.use(route.allOther);
....
当我尝试访问该页面时出现问题:
主机/限制/ MyApp的/
没有填写“datetoget”参数。当我尝试访问此页面时,这是控制台
HTTP Express server listening on port 3000
undefined
GET /restricted/myapp/ 500 8ms
我在myapp之后放一些东西/它有效(例如:/ host / restricted / myapp / something)。 以下是routes.myapp函数的代码的一部分:
function(req,res){
console.log('into myapp'); //this step seems not to be reached
var date;
if(req.params.datetoget){
date = new Date(req.params.datetoget);
if(!utilities.isDate(date)){
date = new Date();
}
}else{
date = new Date();
}
...
}
它似乎也是最后一次
.use(routes.allOther)
不起作用,因为我是标准的500快递错误页面。 我也尝试过像这样的新路线
.get('/restricted/myapp/', routes.myapp)
它没有解决问题。
以防万一这是routes.datePicker
var notInDir = true;
for(var dirKey in directories){ //directories is an object where I store string of directories
var dir = directories[dirKey];
if(req.originalUrl === dir.ref){ //The origin must be known
var datePicker = {
title:"Select the date",
jsFile: [array of Javascript needed files],
cssFile: [array of css needed files],
mode: "datebox",
ref: req.originalUrl
};
res.render("/restricted/datepicker.ejs",datePicker);
notInDir = false;
}
}
if(notInDir){
throw "Error";
}
到目前为止,我有点失落,有点帮助会很感激:-) 非常感谢。
答案 0 :(得分:0)
看来快递考虑了这条路线:
/restricted/myapp
和这条路线:
/restricted/myapp/
同样如此。所以调用的函数是routes.datePicker。但在我的情况下,原点(/ restricted / myapp /)没有在''目录''中定义,只有/ restricted / myapp,这就是抛出异常的原因。