TypeError:未定义节点js的'writeHead'

时间:2017-10-25 12:14:45

标签: node.js

我在node.js. WriteHead()函数中的新功能不断给我typeError。虽然我使用了response.wrtieHead(200,{'Context-Type':'text/plain'})并给出了相同的错误。请帮忙。 这是我的代码。

    var http = require('http');
    var fs = require('fs');
    function send404Response(response){
    response.writeHead(200,{"content-Type":"text/plain"});
    response.write("404 paage");
    response.end();

    }

    function onRequest(request,response){

    if(response.method== 'GET' && response.url('/') ){

      response.writeHead(200, {"content-Type":"text/html"});
      fs.createReadStream("./index.html").pipe(response);
    }
    else{
      send404Response();
    }

  }
  http.createServer(onRequest).listen(8888);
  console.log("server is running");

1 个答案:

答案 0 :(得分:1)

我想您忘记在send404Response函数中发送回复:

    var http = require('http');
    var fs = require('fs');

    function send404Response(response){
        response.writeHead(200,{"content-Type":"text/plain"});
        response.write("404 paage");
        response.end();
    }

    function onRequest(request,response){

        if (request.method== 'GET' && request.path == '/') {
            response.writeHead(200, {"content-Type":"text/html"});
            fs.createReadStream("./index.html").pipe(response);
        }
        else{
            send404Response(response);
        }

    }
    http.createServer(onRequest).listen(8888);
    console.log("server is running");

希望它有所帮助!