Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Node.js is a popular runtime platform to create programs that run on it.
It lets us run JavaScript outside the browser.
In this article, we’ll look at how to start using Node.js to create programs.
HTTP
The http
module is a module that comes with the Node.js runtime environment.
We can use it to create a simple HTTP server.
All we have to do is to require the HTTP package by writing:
const http = require('http');
const server = http.createServer();
server.listen(8080, () => {
console.log( 'Listening on port 8080' );
});
We require the HTTP module and then call the createServer
method on the imported module to create our server.
Then we call the listen
method on the server
object to listen for requests on port 8080.
If we want to to handle requests and return responses, we can use the request
and response
parameters to get the request data and create the response respectively.
For example, we can write:
const http = require('http');
let count = 0;
const server = http.createServer((request, response) => {
count += 1;
response.writeHead(201, {
'Content-Type': 'text/plain'
});
const message = `count: ${count}`;
response.end(message);
});
server.listen(8080, () => {
console.log('Listening on port 8080');
});
We use the methods in the response
object to create the response that we return to the user.
We call the writeHead
method with the status code and the response headers that we want to return.
The Content-Type
header is set to text/plain
so that we can let the client know what the data type of the response is.
201 status means a new object is created.
The response.end
method lets us return the response body to the client.
To get request data, we can use the properties of the request
parameter.
For example, we can write:
const http = require('http');
let count = 0;
const server = http.createServer((request, response) => {
count += 1;
response.writeHead( 201, {
'Content-Type': 'text/plain'
});
const message = `count: ${count}, path: ${request.url}`;
response.end(message);
});
server.listen(8080, () => {
console.log('Listening on port 8080');
});
to get the path of the URL with the request.url
property and return it with the response body.
Routing
Since we have access to the URL that the request is made to, we can do something according to the URL that the user is accessing.
For example, we can write:
const http = require('http');
let count = 0;
const server = http.createServer((request, response) => {
let message,
status = 200;
count += 1;
switch (request.url) {
case '/count':
message = count.toString();
break;
case '/hello':
message = 'World';
break;
default:
status = 404;
message = 'Not Found';
break;
}
response.writeHead(status, {
'Content-Type': 'text/plain'
});
response.end(message);
});
server.listen(8080, () => {
console.log('Listening on port 8080');
});
to send different responses according to the URL that is given.
If request.url
is /count
then we return the value of the count
with the 200 status.
If request.url
is /hello
, then we return 'World'
with the 200 status.
Otherwise, we return 'Not Found'
with a 404 status.
Conclusion
We can create a simple HTTP server with the Node.js’s built-in http
module.