Nodejs - 重定向网址 [英] Nodejs - Redirect url

查看:43
本文介绍了Nodejs - 重定向网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get a node.js server to redirect users to a 404.html page when they enter an invalid url?

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

I did some searching, and it looks like most results are for Express, but I want to write my server in pure node.js.

推荐答案

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

The logic of determining a "wrong" url is specific to your application. It could be a simple file not found error or something else if you are doing a RESTful app. Once you've figured that out, sending a redirect is as simple as:

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

这篇关于Nodejs - 重定向网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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