Geçersiz bir url girdiklerinde kullanıcıları bir 404.html sayfasına yönlendirecek bir node.js sunucusunu nasıl edinebilirim?
Biraz arama yaptım ve sonuçların çoğu Express için gibi görünüyor, ancak sunucumu saf node.js'de yazmak istiyorum.
Geçersiz bir url girdiklerinde kullanıcıları bir 404.html sayfasına yönlendirecek bir node.js sunucusunu nasıl edinebilirim?
Biraz arama yaptım ve sonuçların çoğu Express için gibi görünüyor, ancak sunucumu saf node.js'de yazmak istiyorum.
Yanıtlar:
"Yanlış" bir url belirleme mantığı uygulamanıza özeldir. Bir RESTful uygulaması yapıyorsanız, basit bir dosya bulunamadı hatası veya başka bir şey olabilir. Bunu anladıktan sonra, yönlendirme göndermek şu kadar basittir:
response.writeHead(302, {
'Location': 'your/404/path.html'
//add other headers here...
});
response.end();
Location: /ve sizi kök klasöre yönlendirir.
'Location': '/path/to/my/servers/content'
Kullanmak mümkündür:
res.redirect('your/404/path.html');
Eksik bir dosyayı / kaynağı belirtmek ve bir 404 sayfası sunmak için yeniden yönlendirmenize gerek yoktur. Aynı istekte, durum kodu 404 olarak ayarlanmış ve yanıt gövdesi olarak 404 HTML sayfanızın içeriği ile yanıtı oluşturmalısınız. İşte bunu Node.js'de göstermek için örnek kod.
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);
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!
res.writeHead(302, {'Location': 'https://example.com' + req.url});
res.end();
Sadece bunu nerede kullandığınızı düşünün (örneğin sadece http isteği için), böylece sonsuz yeniden yönlendirmeler almazsınız ;-)
Varsayılan 404 olan bir switch deyimi kullandım:
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);
Bunu dene:
this.statusCode = 302;
this.setHeader('Location', '/url/to/redirect');
this.end();
Expresssaf node.jskod için sorulan bir kod parçacığıdır
thisparça dışında vanilya Düğüm koduna benziyor . NodeJS'de iki parametreyi kabul eden bir işleyici yazarsınız ve yanıttaki (request, response)tüm bu çağrılar responsenesne üzerindedir.
Aşağıdaki kodu kullanmanız gerekir:
response.writeHead(302 , {
'Location' : '/view/index.html' // This is your url which you want
});
response.end();
Native Nodejs'de düzgün çalışan aşağıdaki kodu kullanın
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);