Node.js server “404 not found” message to 404.html page

前端 未结 5 1885
南旧
南旧 2021-01-04 16:50

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 = r         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-04 16:55

    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

提交回复
热议问题