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 :
// 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);
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).
If you actually want to send a request on end
event, you can consume the stream with Streams 2 APIs:
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);
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With