Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My http.createserver in node.js doesn't work?

Hello guys i just started learning node.js today and search a lot off stuff on the internet , then try to code in node.js i use these two codes to show me the same result but the last one is show the error on my browser something likes "can not find the page".So please explain to me why?

// JScript source code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

This is working but

// 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(1337, "127.0.0.1");

This one is not working

Why?

The link of the last one that's not working http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/ Thank you for all the answers, but i still don't understand about the problems. the last one that is not working just has request.on?

like image 434
Sarin Suriyakoon Avatar asked Oct 14 '13 15:10

Sarin Suriyakoon


People also ask

What is createServer in node js?

The createServer method creates a server on your computer: var http = require('http'); http. createServer(function (req, res) { res.

Is HTTP built in node js?

Node. js has a built-in module called HTTP, which allows Node. js to transfer data over the Hyper Text Transfer Protocol (HTTP).

Can I use Apache with node js?

The benefits Apache brings to Node. js applications. How to configure Apache for a Node.


2 Answers

request is an instance of http.IncomingMessage, which implements the stream.Readable interface.

Documentation at http://nodejs.org/api/stream.html#stream_event_end says:

Event: 'end'

This event fires when no more data will be provided.

Note that the end event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by calling read() repeatedly until you get to the end.

var readable = getReadableStreamSomehow();
readable.on('data', function(chunk) {
  console.log('got %d bytes of data', chunk.length);
})
readable.on('end', function() {
  console.log('there will be no more data.');
});

So in your case, because you don't use either read() or subscribe to the data event, the end event will never fire.

Adding

 request.on("data",function() {}) // a noop

within the event listener would probably make the code work.

Note that using the request object as a stream is only necessary for when the HTTP request has a body. E.g. for PUT and POST requests. Otherwise you can consider the request to have finished already, and just send out the data.

If the code your posting is taken literally from some other site, it may be that this code example was based on Node 0.8. In Node 0.10, there have been changes in how streams work.

From http://blog.nodejs.org/2012/12/20/streams2/

WARNING: If you never add a 'data' event handler, or call resume(), then it'll sit in a paused state forever and never emit 'end'. So the code you posted would have worked on Node 0.8.x, but does not in Node 0.10.x.

like image 152
Myrne Stol Avatar answered Oct 06 '22 19:10

Myrne Stol


The function you are applying to the HTTP server is the requestListener which supplies two arguments, request, and response, which are respectively instances of http.IncomingMessage and http.ServerResponse.

The class http.IncomingMessage inherits the end event from the underlying readable stream. The readable stream is not in flowing mode, so the end event never fires, therefore causing the response to never be written. Since the response is already writable when the request handler is run, you can just directly write the response.

var http = require('http');

http.createServer(function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.end('Hello HTTP!');
}).listen();
like image 41
hexacyanide Avatar answered Oct 06 '22 18:10

hexacyanide