gpt4 book ai didi

node.js - 如何捕获nodejs连接静态404错误?

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:01 25 4
gpt4 key购买 nike

我使用nodeJS和senchalab的连接中间件(不是express!!)。我需要捕获静态错误(如 404)。我怎样才能得到这些?我尝试检查源代码,但找不到将错误处理程序传递给 connect.static 的方法。

这是我(必须)使用的一般结构的要点:

https://gist.github.com/2397415

最佳答案

根据您的路线:

router.routes['/'] = 'my page';
router.routes['/404'] = '404 test';

我认为你的意思是:

connect()
.use(connect.static(__dirname + '/public'))
.use(function(req, res, next){
switch (req.url) {
case '/404':
var body = '404 test';
res.statusCode = 404;
res.setHeader('Content-Length', body.length);
res.end(body);
break;
default:
var body = 'my page';
res.setHeader('Content-Length', body.length);
res.end(body);
}
})
.listen(3001);

我还想补充一点,在旧的连接版本 1.x 中,您可以使用“connect.router”,但在 2.x 版本中,它是 removed并感动地表达。如果您需要有用的路由系统,请使用express:

Connect was never meant to be used directly, it exists to support frameworks like Express

关于node.js - 如何捕获nodejs连接静态404错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170960/

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