How create a few servers by using Node.js
How create a few servers by using Node.js
In general, they say that a web server is a program that is responsible for organizing the response to user requests sent by their HTTP clients/agents. There are many of Web Server types
In current article we will talking about servers based on Node.js engine works under Unix-like OS only.
Creating a few servers using Node.js engine
Node.js cross-platform powerful runtime environment based on V8 engine and intended for executing server-site JavaScript (sometimes named ECMAScript). There are many articles about creating a basic HTTP server based on Node.js built-in module called HTTP. The HTTP module contains the function to create the server object (createServer()), bind it to a network address (listen()) and finally starts the HTTP server listening for connections. So, the HTTP Server object can listen now to ports on your computer and execute a special function named callback (requestListener()) each time a request to Server is made.
Some a simplest example is depicted below:
It can be run by command
Requesting by using curl command or any browser is showing the beforehand waited response:
Well, all is OK for now.
But what we should do if we need to have in run a few different servers in parallel? Does it possible to do by using only one Node.js engine?
Actually, turns out, it is possibly because Node.js allows create as many server objects as you want, by using practically even various Hypertext Transfer Protocols (HTTP, HTTPS, HTTPS with Client Authentication, etc.)
By the way, every server can have its own requestListener callback function that will processing corresponding requests and preparing responses.
Take also into consideration a several important aspects while creation of a few web servers by one Node.js engine:
advantages
Clustering servers using Node.js
If your Web Server is starting to receive a huge amount of traffic it's much possibly may your underlying server couldn't be handle it. Typically, adding a few servers can solve this issue. But in order to share traffic to your all servers, a load balancing is required. Usually, a load balancer is sitting in front of Web Servers and routing (by using some policy) a client requests across all servers that minimizes time of requests answered and capacity utilization.
Node.js engine has a built-in module called Cluster Module to take the advantage of a multi-core processor. Using this module you can launch Node.js servers to each core of your system. Master process listening an outed available port to accept client requests and distribute across the worker-servers using round-robin routing.
Note that load balancing provides so named horizontal scalability by adding more server nodes or resources to handle increased traffic. Clustering module offers vertical scalability by adding more servers or resources onto one machine to handle increased demands. Since every server will work on separate processor core, you can have as much servers as many core has your processor.
领英推荐
Few servers creation simple example
The simple Node.js project that shows how to create a various servers can be found on GitHub.
Everyone can download and try it without any restriction.
Project creates 3 kind of servers simultaneously - HTTP, ordinary HTTPS and HTTPS with client authentication.
Project includes some Bash scripts as well which helps you to create self-signed certificates for HTTPS server and client authentication.
Downloaded project contains 2 main scrips:
The basic steps can be found in Project ReadMe file.
Evaluating the benchmark of server single instance vs clustering mode
Start one instance of server.js script by using the following command:
node --openssl-legacy-provider server.js
Note that NODE_OPTIONS environment variable --openssl-legacy-provider is needed only if you generated and use PFX certificate and Node.js version you use is above than 17.x.
To see the performance, we have used autocannon tools to benchmark test our server as shown below:
The above screenshot showed that the server can respond up to 103000 requests when running 500 concurrent connections during 10 seconds. The average requests/second is 10205.
Now starting the same server.js script under cluster mode with 2 worker instances.
node --openssl-legacy-provider cluster.js
The performance was evaluated in the same way, by using autocannon tools. So the result shown below:
The above screenshot showed that 2 servers under cluster can respond up to 178000 requests when running 500 concurrent connections during 10 seconds. The average requests/second is 17714.
Thus, using the cluster mode you can handle more requests, in our case in about 73.5% more.
Summary
Summarizing above mentioned, it can be say that without any doubts it is possible to creation a few servers on one Node.js engine.
This technique can be even used in production. For instance, I had some project where 3 servers on clustered Node.js were established:
But, of course, practically, this technique most commonly uses for short development that is very suitable while pilot projects, when require to check some idea or supposition.