node.js http://localhost:8080/ 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19608330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
http://localhost:8080/ is not working
提问by user sks
I am new to nodeJS and triyng to learn it.
I am trying to execute hello world example from http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/but i am not getting any output and i am getting No data received page on chrome browser.
I have installed apache (XAMPP) on my PC but it is not active and also when i am trying to run node http.jsin terminal i am not getting any output.
I have one another file,hello.js which contains console.log('Hello World!');when i have run node hello.jsi am getting Hello World!output in terminal.
But http.jsis not working.
http.js code :
我是 nodeJS 的新手并试图学习它。
我正在尝试从http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/执行 hello world 示例,
但我没有得到任何输出,而且我在 chrome 上没有收到任何数据页面浏览器。
我已经在我的 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);
回答by vkurchatkin
I suppose you use node 0.10.x or later? It has some changes in streamapi, often referred as Streams2. One of the new features in Streams2 is that endevent is never fired until you consume the stream completely (even if it is empty).
我想您使用节点 0.10.x 或更高版本?它在streamapi 中有一些变化,通常称为 Streams2。Streams2 中的一项新功能是在end您完全使用流(即使它是空的)之前永远不会触发事件。
If you actually want to send a request on endevent, you can consume the stream with Streams 2 APIs:
如果您真的想发送end事件请求,您可以使用 Streams 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);
or you can switch stream into old (flowing) mode:
或者您可以将流切换到旧(流动)模式:
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);
Otherwise, you can send response right away:
否则,您可以立即发送响应:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.end('Hello HTTP!');
}).listen(8080);
回答by Dharmender Tuli
Try this
尝试这个
//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);
});

