我是 nodeJS 的新手,并试图学习它。
我正在尝试从http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/执行 hello world 示例,
但我没有得到任何输出,并且我在 chrome 上收到 No data received 页面浏览器。
我已经在我的 PC 上安装了 apache(XAMPP),但它没有激活,而且当我尝试node http.js
在终端中运行时,我没有得到任何输出。
我有另一个文件,hello.js,其中包含console.log('Hello World!');
我运行时在终端中node hello.js
获取输出。Hello World!
但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);