gpt4 book ai didi

node.js - Nodejs - 重定向网址

转载 作者:IT老高 更新时间:2023-10-28 21:47:33 28 4
gpt4 key购买 nike

当用户输入无效的 url 时,如何让 node.js 服务器将用户重定向到 404.html 页面?

我做了一些搜索,看起来大多数结果都是针对 Express 的,但我想用纯 node.js 编写我的服务器。

最佳答案

确定“错误”网址的逻辑特定于您的应用程序。如果您正在做一个 RESTful 应用程序,它可能是一个简单的文件未找到错误或其他问题。一旦你弄清楚了,发送重定向就像这样简单:

response.writeHead(302, {
'Location': 'your/404/path.html'
//add other headers here...
});
response.end();

关于node.js - Nodejs - 重定向网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062260/

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