2013-10-26 120 views
9

我是新來的nodeJS和triyng來學習它。
我試圖執行來自http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/ hello世界的例子,但我沒有得到任何輸出,我在Chrome瀏覽器上沒有收到數據接收頁面。
我已經在我的電腦上安裝了apache(XAMPP),但它並不活躍,並且當我試圖在終端中運行node http.js時,我沒有收到任何輸出。
我有另一個文件,hello.js其中包含console.log('Hello World!'); 當我運行node hello.js我在終端輸出Hello World!。 但http.js不起作用。
http.js代碼:http:// localhost:8080 /不工作

// Include http module. 
var http = require("http"); 

// Create the server. Function passed as parameter is called on every request made. 
// request variable holds all request parameters 
// response variable allows you to do anything with response sent to the client. 
http.createServer(function (request, response) { 
    // Attach listener on end event. 
    // This event is called when client sent all data and is waiting for response. 
    request.on("end", function() { 
     // Write headers to the response. 
     // 200 is HTTP status code (this one means success) 
     // Second parameter holds header fields in object 
     // We are sending plain text, so Content-Type should be text/plain 
     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 
     // Send data and end response. 
     response.end('Hello HTTP!'); 
    }); 
// Listen on the 8080 port. 
}).listen(8080); 
+1

您是否在控制檯中發現任何錯誤?關於發生了什麼的任何提示?當我在瀏覽器中轉到localhost:8080時,此代碼適用於我。你有沒有嘗試在瀏覽器中輸入:http://127.0.0.1:8080?在我的瀏覽器中,http://127.0.0.1:8080和http:// localhost:8080都適用於我,但您需要在url中指定8080端口。 – thtsigma

+0

你使用代理服務? –

+0

控制檯中沒有錯誤,我在這裏沒有使用代理。 –

回答

26

我想你使用節點0.10.x或更高版本?它在stream API中有一些變化,通常被稱爲Streams2。 Streams2中的一項新功能是,直到完全使用流(即使它爲空),纔會啓動事件end

如果你真的想在end事件發送一個請求,你可以消耗與流2的API流:

var http = require('http'); 

http.createServer(function (request, response) { 

    request.on('readable', function() { 
     request.read(); // throw away the data 
    }); 

    request.on('end', function() { 

     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 

     response.end('Hello HTTP!'); 
    }); 

}).listen(8080); 

,或者你可以切換流成舊的(流動)模式:

var http = require('http'); 

http.createServer(function (request, response) { 

    request.resume(); // or request.on('data', function() {}); 

    request.on('end', function() { 

     response.writeHead(200, { 
     'Content-Type': 'text/plain' 
     }); 

     response.end('Hello HTTP!'); 
    }); 

}).listen(8080); 

否則,您可以發送迴應的時候了:

var http = require('http'); 

http.createServer(function (request, response) { 

    response.writeHead(200, { 
    'Content-Type': 'text/plain' 
    }); 

    response.end('Hello HTTP!'); 
}).listen(8080); 
2

試試這個

//Lets require/import the HTTP module 
var http = require('http'); 

//Lets define a port we want to listen to 
const PORT=8080; 

//We need a function which handles requests and send response 
function handleRequest(request, response){ 
    response.end('It Works!! Path Hit: ' + request.url); 
} 

//Create a server 
var server = http.createServer(handleRequest); 

//Lets start our server 
server.listen(PORT, function(){ 
    //Callback triggered when server is successfully listening. Hurray! 
    console.log("Server listening on: http://localhost:%s", PORT); 
}); 
相關問題