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}/`); });
Hit the RUN button to view the output
The server-side
- We import the
http
module on line 1. - We declare our
hostname
andport