gpt4 book ai didi

node.js - 在 Express 中使用路由时出现错误 500

转载 作者:太空宇宙 更新时间:2023-11-04 02:36:55 24 4
gpt4 key购买 nike

我正在尝试使用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);
....

当我尝试访问该页面时出现问题:

主机/restricted/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";
}

到目前为止我有点迷失,希望得到一些帮助:-)非常感谢。

最佳答案

看来express正在考虑这条路线:

/restricted/myapp

这条路线:

/restricted/myapp/

一样。所以调用的函数是routes.datePicker。但就我而言,来源(/restricted/myapp/)没有在“目录”中定义,只有/restricted/myapp,这就是抛出异常的原因。

关于node.js - 在 Express 中使用路由时出现错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com