Node.js服务器“404未找到”消息到404.html页面 [英] Node.js server "404 not found" message to 404.html page

查看:997
本文介绍了Node.js服务器“404未找到”消息到404.html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js,我想知道如何显示404.html而不是404 Not Found消息。



这是我的server.js:

  var http = require(http),
url = require(url) ,
path = require(path),
fs = require(fs)
port = process.argv [2] || 8888;

http.createServer(function(request,response){

var uri = url.parse(request.url).pathname
,filename = path.join (process.cwd(),uri);

path.exists(filename,function(exists){
if(!exists){
response.writeHead(404,{ Content-Type:text / plain});
response.write(404 Not Found \\\
);
response.end();
return; $ b (fs.statSync(filename).isDirectory())filename + ='public / Index / index.html';

fs.readFile(filename ,binary,function(err,file){
if(err){
response.writeHead(500,{Content-Type:text / plain});
response.write(err +\ n);
response.end();
return;
}

response.writeHead(200);
response.write(file,binary);
response.end();
});
});
})。listen(parseInt(port ,10));
$ b console.log(静态文件服务器运行在\\\
=> http:// localhost:+ port +/ \ nCTRL + C关闭);

你可以看到它只是一个静态文件服务器,我没有使用express.js或任何东西。

解决方案

p>

  response.writeHead(404,{Content-Type:text / plain}); 
response.write(404 Not Found \\\
);
response.end();

您可以更改为

  response.writeHead(404,{Content-Type:text / html}); 
response.write(HTMLDATA);
response.end();

'HTMLDATA'既可以是HTML的字符串,也可以是对您已经收集到的文件。
$ b response.writeHead()总是在响应之前设置.write()



另请参阅我们已将响应类型设置为'text / html'




http://nodejs.org/api/http .html#http_class_http_serverresponse


Im working with node.js and I would like to know how to display a 404.html instead of a "404 Not Found" message.

This is my server.js:

var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;

http.createServer(function(request, response) {

var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);

path.exists(filename, function(exists) {
if(!exists) {
  response.writeHead(404, {"Content-Type": "text/plain"});
  response.write("404 Not Found\n");
  response.end();
  return;
}

if (fs.statSync(filename).isDirectory()) filename += 'public/Index/index.html';

fs.readFile(filename, "binary", function(err, file) {
  if(err) {        
    response.writeHead(500, {"Content-Type": "text/plain"});
    response.write(err + "\n");
    response.end();
    return;
  }

  response.writeHead(200);
  response.write(file, "binary");
  response.end();
  });
 });
}).listen(parseInt(port, 10));

console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown");

as you can see its just a static file server and I'm not using express.js or anything.

解决方案

H i ,

within your 404 case

  response.writeHead(404, {"Content-Type": "text/plain"});
  response.write("404 Not Found\n");
  response.end();

You can change to

  response.writeHead(404, {"Content-Type": "text/html"});
  response.write(HTMLDATA);
  response.end();

'HTMLDATA' being either a string of HTML or a reference to a file you have gathered.

response.writeHead() is always set before the response.write().

Also see we have set the response type to 'text/html'


http://nodejs.org/api/http.html#http_class_http_serverresponse

这篇关于Node.js服务器“404未找到”消息到404.html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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