HTTP
Learn how to make a web server with the HTTP module in Node.js.
We'll cover the following
The web
Websites, much like this one, don’t use a command-line terminal to serve all those beautiful web pages. Simple servers and clients that work using the terminal exist on the transport layer, whereas HTTP resides on the application layer of the internet protocol suite.
. This is the layer users interact with using a website’s front-end. Node.js allows us to serve web pages using the http
module. Let’s see how a simple server can be made in just a few lines of code:
const http = require('http'); const hostname = '0.0.0.0'; const port = 3500; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('Hello World'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
The server-side
- We import the
http
module on line 1. - We declare our
hostname
andport
on line 3 and line 4. - The
createServer
method on line 6 creates anhttp.Server
instance. ArequestListener
function is also added as a callback here. TherequestListener
is automatically to theadded will be fired when a request event is raised request
event. Therequest
event returns two objects: arequest
and aresponse
. Therequest
event is automatically fired when a client requests something from the server. In our case, the client simply requests for the web page to load. - We use
req
andres
as the names for the request and response returned by therequest
event. The request is an instance of thehttp.IncomingMessage
class, and the response is an instance of thehttp.ServerResponse
class. We do not use thereq
for this simple demo; instead, we focus on theres
. - The
statusCode
method on line 7 sets the HTTP status code. We set it to 200. If you are not familiar with HTTP status codes, 200 translates to OK. You might be familiar with 404, which translates to Not Found. - HTTP headers let the client and the server pass additional information with an HTTP request or response. There is a standard for these headers; we will use the
Content-Type
header and set it totext/html
on line 8. - The
end
method tells the server that all of the response headers and body have been sent. The server now knows that the message is complete. This method must be called on each response. We pass it our data, Hello World as the only parameter; however, we can also pass it a callback function that will run after the response ends. We will see how that works in a later lesson. - The
listen()
method is identical to theserver.listen()
method which is used in thenet
module. Now, the server can start accepting requests from clients. We are also using a callback function to output thehostname
and theport
on the console.
Web servers are much more complex than the one we made in the code widget above. Ideally, for a lot of this code, we have libraries and modules that make building web applications a lot easier.
Get hands-on with 1300+ tech skills courses.