如何使用 Express/Node 以编程方式发送 404 响应? [英] How to programmatically send a 404 response with Express/Node?

查看:15
本文介绍了如何使用 Express/Node 以编程方式发送 404 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Express/Node 服务器上模拟 404 错误.我该怎么做?

I want to simulate a 404 error on my Express/Node server. How can I do that?

推荐答案

从 Express 4.0 开始,就有了专门的 sendStatus 函数:

Since Express 4.0, there's a dedicated sendStatus function:

res.sendStatus(404);


如果您使用的是早期版本的 Express,请使用 status 函数 代替.


If you're using an earlier version of Express, use the status function instead.

res.status(404).send('Not found');

这篇关于如何使用 Express/Node 以编程方式发送 404 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆