0

I am trying to create server using 'http' module of node but when i try to run the code using node the node got stuck and nothing is happening.

Also I got this message under console when i try to open the server using link 'http://localhost:8080/'

Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING

const http = require('http');

const server = http.createServer((req,res)=>{
    res.write('hello');
});


server.listen(8080);

ScreenShots: Source Code

Console

SiIconic
  • 41
  • 3
  • The same question is addressed here : https://stackoverflow.com/questions/29894154/chrome-neterr-incomplete-chunked-encoding-error – Sagar Kulkarni May 27 '20 at 06:30
  • I think you're missing `res.end()` after the `res.write()`: https://nodejs.org/api/http.html#http_response_end_data_encoding_callback. You can simplify it by removing the explicit `write()` and just doing `res.end('hello');`. – mirage May 27 '20 at 06:35

1 Answers1

0

You need to explicitly end the response using response.end().

From the documentation:

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

This function also takes a data argument, so you can use this to send the message to the client.

const http = require('http');

const server = http.createServer(function (req, res) {
    res.end('hello');
});

server.listen(8080)
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40