top of page
Search

Web Servers in Node.js

Writer's picture: Sachin TahSachin Tah


The term "Web Server" is a little scary to all of us since application programmers are not wired to write Web Servers, they are supposed to write programs, APIs, services that will be hosted and eventually run on a Web Server. So slight changes in jumper settings are required in a programmer's circuit :).

But why should I write a web server in Node.js? Can't I directly run my node program on any of the existing web servers like Apache or IIS? The answer as of today is a big NO.

What is a Web Server?

A Web Server is typically a program that accepts Web HTTP requests on a specific port (default 80) and sends a response back to the requestor. It interprets your request and serves you the requested content, either static contents like HTML, images, CSS or dynamic contents which is interpreted by your server via language interpreter configured in it.

Implementing REST API

Let us try to write a simple REST API and host it on Node.js web server.

REST APIs have gained great popularity and are indeed considered as industry best practices for implementing service-based or micro service architecture.

Node.js is one of the coolest platforms I have ever seen for implementing high performance and scalable REST APIs. You can use any database of your choice be it conventional relational databases like MySQL and Oracle or No SQL databases like Mongo and AeroSpike, REST APIs developed in Node.js works like a charm and by default are platform-independent. We have developed thousands of complex REST APIs for our customers using Node.js and trust me, they are highly stable and work on almost all platforms without changing a single line of code, and deployment is as good as copying files from one PC to another.

You can write a simple REST API using Node and make it accessible to others, you need to host in on a server written in Node.

Amazingly you can do all this with only 5 to 10 lines of code.

Concurrency in Web Server using Single Thread Node.js?

As we know Node.js works on a single thread, so how can we write a web server which will handle multiple concurrent users and sends a response back to them? To make our life easy, there is a ready-made library available called express.js which can be used to handle various HTTP tricks with ease, you can download the latest version of express using npm

Server.js

Create a text file and name it Server.js (you can give any name to your file), this Server.js is the entry point to you API server

//--Server.js--

var http = require('http'); 


var express = require('express' , http);
var app = express();

//--Allow Requests from All Domains--
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});
app.get('/samples/servercheck/', function(req,res){res.send("server up...");res.end;});

app.listen(8080);

Download express using npm, http is one of the core libraries of Node.js so you can use it directly. While creating an instance of express you need to pass instances of http object so that it will be configured to work with HTTP protocol.


app.listen(8080) will open a new port (if available) and your node application will start listening to requests on this port. We have implemented a simple REST API which accepts requests on HTTP GET, if called it returns "server up", you can test the same using either POSTMAN or web browser by giving URL http://localhost:8080/samples/servercheck/


We have created a Node.js Web Server hosting REST API in 5-10 lines of code and that too self-hosted means it does not require an external Web Server to host itself. That does not mean that you can not use Apache or any other Web server to host Node.js, you can do it but via a reverse proxy mechanism which I will cover in some other post.

Remember if you are planning to deploy this on a production server, you may need to make some extra settings in your application firewall to make this port accessible outside.

18 views0 comments

Recent Posts

See All

Comments


bottom of page