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.js
in 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.js
i am getting Hello World!
output in terminal. But http.js
is not working.
http.js code :
我是nodeJS和triyng的新手来学习它。我试图从http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/执行hello world示例,但我没有得到任何输出,我得到没有数据接收页面上的chrome浏览器。我已经在我的电脑上安装了apache(XAMPP),但它没有激活,而且当我试图在终端中运行节点http.js时,我没有得到任何输出。我有另一个文件,hello.js,其中包含console.log('Hello World!');当我运行节点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);
2 个解决方案
#1
26
I suppose you use node 0.10.x or later? It has some changes in stream
api, often referred as Streams2. One of the new features in Streams2 is that end
event is never fired until you consume the stream completely (even if it is empty).
我想你使用节点0.10.x或更高版本?它在流api中有一些变化,通常称为Streams2。 Streams2的一个新功能是,在您完全使用流(即使它是空的)之前,永远不会触发结束事件。
If you actually want to send a request on end
event, you can consume the stream with Streams 2 APIs:
如果您确实想要在结束事件上发送请求,则可以使用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);
#2
4
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);
});
#1
26
I suppose you use node 0.10.x or later? It has some changes in stream
api, often referred as Streams2. One of the new features in Streams2 is that end
event is never fired until you consume the stream completely (even if it is empty).
我想你使用节点0.10.x或更高版本?它在流api中有一些变化,通常称为Streams2。 Streams2的一个新功能是,在您完全使用流(即使它是空的)之前,永远不会触发结束事件。
If you actually want to send a request on end
event, you can consume the stream with Streams 2 APIs:
如果您确实想要在结束事件上发送请求,则可以使用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);
#2
4
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);
});