Node.js http module

When we order chicken, we say ‘it is A apartment in B area, … please deliver us a chicken’ then we get a chicken after some time in the right place where we talked.

In this situation, putting URL on web browser is ordering chicken and getting the web page from the web server is getting the chicken from the restaurant. It’s the basic understanding about request and response

Depending on the way to send the ordering, we can call the server as HTTP web server or HTTPS web server etc.
When we use request message, we get proper web page for users.
When we use response message, we can save and take cookies and force to move the page.
In http module, the most important object is server. we can declare server by using createServer() method.

Server object’s method

1
2
listen(port[, callback]) — Start server
close([callback]) — Stop server

Server object’s event

1
2
3
4
5
6
request — when client asks something
connection — when client opens the server
close — when the server is closed
checkContinue — when client keeps connecting the server
upgrade — when client asks HTTP upgrade
clientError — when client makes errors

When we want to provide an web page, we need to write response message. And at that time, we use response object which is the 2nd parameter from request event listener.

Response object’s method

1
2
writeHead(statusCode[, statusMessage][, headers]) — write down the response header
end([data][, encoding][, callback]) — write the main response content

Comments