Nodejs - 重定向网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4062260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs - Redirect url
提问by Dzung Nguyen
How do I get a node.js server to redirect users to a 404.html page when they enter an invalid url?
当用户输入无效 url 时,如何让 node.js 服务器将用户重定向到 404.html 页面?
I did some searching, and it looks like most results are for Express, but I want to write my server in pure node.js.
我做了一些搜索,看起来大多数结果都是针对 Express 的,但我想用纯 node.js 编写我的服务器。
回答by Chetan Sastry
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:
确定“错误”网址的逻辑特定于您的应用程序。如果您正在开发 RESTful 应用程序,这可能是一个简单的文件未找到错误或其他错误。一旦你弄清楚了,发送重定向就像这样简单:
response.writeHead(302, {
'Location': 'your/404/path.html'
//add other headers here...
});
response.end();
回答by Dani?l W. Crompton
It's possible to use:
可以使用:
res.redirect('your/404/path.html');
回答by Chandra Sekar
To indicate a missing file/resource and serve a 404 page, you need not redirect. In the same request you must generate the response with the status code set to 404 and the content of your 404 HTML page as response body. Here is the sample code to demonstrate this in Node.js.
要指示丢失的文件/资源并提供 404 页面,您无需重定向。在同一个请求中,您必须生成状态代码设置为 404 的响应,并将 404 HTML 页面的内容作为响应正文。下面是在 Node.js 中演示这一点的示例代码。
var http = require('http'),
fs = require('fs'),
util = require('util'),
url = require('url');
var server = http.createServer(function(req, res) {
if(url.parse(req.url).pathname == '/') {
res.writeHead(200, {'content-type': 'text/html'});
var rs = fs.createReadStream('index.html');
util.pump(rs, res);
} else {
res.writeHead(404, {'content-type': 'text/html'});
var rs = fs.createReadStream('404.html');
util.pump(rs, res);
}
});
server.listen(8080);
回答by Levite
404 with Content/Body
带有内容/正文的 404
res.writeHead(404, {'Content-Type': 'text/plain'}); // <- redirect
res.write("Looked everywhere, but couldn't find that page at all!\n"); // <- content!
res.end(); // that's all!
Redirect to Https
重定向到 Http s
res.writeHead(302, {'Location': 'https://example.com' + req.url});
res.end();
Just consider whereyou use this (e.g. only for http request), so you don't get endless redirects ;-)
只需考虑您在哪里使用它(例如仅用于 http 请求),这样您就不会得到无休止的重定向;-)
回答by Alon and Idan
I used a switch statement, with the default as a 404:
我使用了一个 switch 语句,默认为 404:
var fs = require("fs");
var http = require("http");
function send404Response (response){
response.writeHead(404, {"Content-Type": "text/html"});
fs.createReadStream("./path/to/404.html").pipe(response);
}
function onRequest (request, response){
switch (request.url){
case "/page1":
//statements
break;
case "/page2":
//statements
break;
default:
//if no 'match' is found
send404Response(response);
break;
}
}
http.createServer(onRequest).listen(8080);
回答by Mike
Try this:
尝试这个:
this.statusCode = 302;
this.setHeader('Location', '/url/to/redirect');
this.end();
回答by Arash Keivan
You have to use the following code:
您必须使用以下代码:
response.writeHead(302 , {
'Location' : '/view/index.html' // This is your url which you want
});
response.end();
回答by Krishnamoorthy Acharya
Use the following code this works fine in Native Nodejs
使用以下代码,这在 Native Nodejs 中工作正常
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
if (q.pathname === '/') {
//Home page code
} else if (q.pathname === '/redirect-to-google') {
res.writeHead(301, { "Location": "http://google.com/" });
return res.end();
} else if (q.pathname === '/redirect-to-interal-page') {
res.writeHead(301, { "Location": "/path/within/site" });
return res.end();
} else {
//404 page code
}
res.end();
}).listen(8080);

