English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Node.js Redirect URL: In this Node.js tutorial, we will learn about redirect URLs.
Redirects can be applied in the following situations:
Some resources will be permanently moved to a new location, and you want to redirect the user to the new location of the moved resource.
Some pages in the web application have been deleted, and when the page is requested, you want to redirect the user to the homepage or some custom page.
There are mainly three types of HTTP redirects.
But remember, HTTP redirect codes (such as301,302,307etc.) can affect the page ranking of the original or redirected URL, and the impact of each redirect code is different. For example, if you permanently moved the resource, then in the response use301 The HTTP code will pass the juice to the redirect URL, while302or307it will not.
For the following example, consider two pages: page-a.html and page-b.html, your web application serves them. We have a404_find.html to display when the requested resource does not exist.
In this example, we will demonstrate the scenario where the requested URL must be redirected. When we receive a request for page-When a request for c.html is made, we will send a redirect response to the web client (to find page-b.html)。
var http = require('http'); var fs = require('fs'); // Créer un serveur HTTP http.createServer(function(req, res) { if (req.url == '/page-c.html') { // Use in the response301(Permanent move) Redirect HTTP code to page-b.html res.writeHead(301, { "Location": "http://" + req.headers['host'] + '/page-b.html' }); return res.end(); } else { // For other URLs, please try to respond with the page console.log(req.url) // Read the requested file fs.readFile(req.url.substring(1), function(err, data) { if (err) throw err; res.writeHead(2 res.write(data.toString('utf8); return res.end(); ).listen(8085);
Sortie en terminal
$ node node-js-http-redirect.js
Ouvrez un navigateur, affichez les outils pour développeurs, puis cliquez sur l'URL "http:// localhost:8085/page-c.html
Dans la section "Outils pour développeurs" de "Réseau", vous trouverez que la requête a été redirigée vers une nouvelle page.
Pour la première requête, nous avons envoyé depuis l'application Node.js301Code de réponse.
Dans cet exemple, nous allons démontrer la situation où le fichier demandé n'est pas trouvé. Mais vous ne voulez pas afficher une page ennuyeuse404Page d'erreur. Au lieu de cela, vous souhaitez afficher une autre page, par exemple page-a.html.
var http = require('http'); var fs = require('fs'); // Créer un serveur HTTP http.createServer(function(req, res) { var filePath = req.url.substring(1); fs.readFile(filePath, function(err, data) { // Si une erreur se produit lors de la lecture du fichier, redirigez-le vers la page-b.html if (err) { // lors de la réponse302 Redirection de code HTTP vers page-b.html res.writeHead(302, { "Location": "http://" + req.headers['host'] + '/page-b.html' }); return res.end(); res.writeHead(2 res.write(data.toString('utf8); return res.end(); ).listen(8085);
Sortie en terminal
$ node node-js-http-redirect-file-not-found.js
Ouvrez un navigateur, affichez les outils pour développeurs, puis cliquez sur l'URL "http :// localhost :8085 / page-n.html
Dans la section "Outils pour développeurs" de "Réseau", vous trouverez que la requête a été redirigée vers une nouvelle page, avec302Code HTTP (temporairement déplacé).