2017-10-25 4 views
0

Im in node.js. WriteHead() newはtypeErrorを継続的に与えています。私はresponse.wrtieHead(200,{'Context-Type':'text/plain'})として使用していますが、同じエラーを出しています。助けてください。 ここに私のコードです。TypeError:定義されていないノードの「writeHead」js

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

私はあなたは自分の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"); 

はそれが役に立てば幸い!

+0

ありがとうございました。今実行していますが、404を投げますが、index.htmlファイルに移動する必要があります。@Sparw –

+0

"if"条件の直前にrequest.pathを表示できますか? – Sparw

+0

はい。それは未定義と言います。 –

関連する問題