http:// localhost:8080 /不能正常工作

我是新来的nodeJS和triyng来学习它。
我试图从http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/执行你好世界的例子,但我没有得到任何输出,我得到没有数据接收页上的铬浏览器。
我已经在我的电脑上安装了Apache(XAMPP),但它不是主动的,当我试图在terminal运行node http.js我没有得到任何输出。
我有另一个文件,hello.js其中包含console.log('Hello World!'); 当我运行node hello.js我得到Hello World! 输出在terminal。 但http.js不工作。
http.js代码:

  // 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); 

我想你使用节点0.10.x或更高版本? 它在stream API中有一些变化,通常被称为Streams2。 Streams2中的一个新function是,在完全使用stream(即使它是空的)之前, end事件永远不会被触发。

如果您真的想在end事件中发送请求,则可以使用Streams 2 API使用该stream:

 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); 

或者您可以将stream切换到旧(stream动)模式:

 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); 

尝试这个

 //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); });