(#1) Building production ready backend in 20 mins

post header

Nodejs is a js runtime which is built on chrom’s v8 js engine, there are few remarkable features of nodejs application which makes it choice of 2017 in building server side application. It is based on event-driven, non-blocking I/O model that makes it so lightweight and efficeint you won’t even feel like you are developing a server side application which is going to act as provider and handler for millions of incoming requests. This all comes from the family of ECMAscript which is an object oriented programming language for performing computations and manipulating computational objects within a host environment.

Introduction

Prior to V8 engine host environment was something which synonym with just the browser but ever since the implementation of v8 you have the same browser plus additional feature outside the browser. In node the program never stops that’s what node mindset says. You don’t have to wait for any program to get done or send back the control when done. Nodejs application gives the capability to run js application with server-side capabilities.

There is a latest trend in the developers community to go mobile first, this doesn’t mean you are developing an application which is for mobile app and then for other devices, no what it means is you are developing something which is pluggable for all devices. Prior to this the main focus was on the front, its look and feel, your backend acted like simple dumper and fetcher for your frontend data. This is changing now, we are moving ahead with the more broader scope utilization with nodejs framework, prominently into IOT. Embedded devices are changing everything, you dont need front for everything.

GE Aviation is powering there aircraft with sensors, more like event driven architecture where nodejs is being used. Ebay has already switched to nodejs to handle there huge traffic but when it comes to traffic walmart’s site is the biggest nodejs application in terms of traffic handling. When paypal switched to nodejs for they observed a 2X performance as compared to there java web app and that was when nodejs was just new to the programming world. The recent switch of Linkedin app into a nodejs application is again benchmark achieved by nodejs, did I miss the point of Uber. Well they too have started using nodejs for there application.

Paypal’s tech blog says they observed a 35% decrease in response time for there site. Now that’s a huge plus point for paypal for handling such a gigantic traffic that they observe during a black Friday.
So where can you use nodejs?

JavaScript is everywhere. With JavaScript you can

  • control Internet of Things devices
  • implement real-time services
  • develop web applications
  • operate e-commerce sites
  • serve as backend for your mobile applications enterprise web services

(what are you waiting for when giants are favoring nodejs?) and lot more.

What you get from nodejs?

You can cut short to half the number of developer required to develop a product.
No need for extra servers to handle application, database and separate controlling master backend.
Load time for the page is reduce to 50%
Its always the best developers who endeavor into new tech and explore it to the full
Your customer is always happy with your fast deliveries.
Your developer is also happy with the opportunity to work on the best tech in the market.
You staff stay with you longer since you are providing the best learning environment which is hard to find in the market.

What’s next in this?

If you observe technical trends closely you will observe that enterprises are planning and outsourcing there new requirements into modern technologies like Nodejs and they are caution but desperately want to switch to something faster and reliable. And the best choice in the market is nodejs. Which answers all the question required to handle a full-fledged system keeping a consistent technology both at front and at back. With MEAN stack we only require few hours to prototype and deliver a proof-of-concept in the beginning and move to a full monitored deployment with space for scalability once a green signal is achieved from concerned places.

When you are using traditional server for your web application which is based on thread then you are leveraging each incoming request to a server thread and one thread can handle only one request at a time and you can have only limited number of threads running in your web application server. If you have a huge server which gives you a response in under half a millisecond then you dont need to even think of node but this is not the real case scenario. Companies usually dont have such huge server to concurrently handle request on each thread. When it comes to nodejs it runs on a single thread and dynamically changes the traditional architecture by creating an event loop in the system this event loop handles all the incoming request and give each request a callback which is a c++ pointer and then it moves on to the next incoming request and so on no incoming request have to wait to get the response back to the request.

Once the incoming request is assigned a c++ pointer which is also called a callback. The task in hand of every request is passed on to operating system’s POSIX async threads which makes it so Non-blocking IO system. No matter how slow your db is your system will not break if new request keeps on coming in.

nodejs stack

If you look at the structure of the stack of nodejs you will observe that js is present only on the topmost layer rest all is built in c/c++. so the application layer interacts with nodejs standard library which is built using js and its binding which helps you create socket, http and other activities is built using c/c++ which again forward to the V8 engine which has a performance library names Libuv (earlier it was libeio and libev but libuv is the latest upgrade that V8 engine encountered). From then on the task is handled by multiple kernal threads.

Gone are those days when you had to serialize the data coming from frontend to backend and then to database. These days when the fullstack development is at full craze you can have the same data model at your frontend app, at your backend and at your database allowing you to focus more on quality development rather then technical issues which could come up with other tech stack.

Do you still have doubt about nodejs?

Here is the chart showing number of modules available in the nodejs, the count is more than any other platform.

nodejs stack

nodejs stack

Getting started with Loopback to build your REST api.
Loopback is a nodejs framework build by strongloop an IBM owned company. Strongloop have the biggest pie share for contributing to nodejs repo.
So what does loopback help you with in this whole setup? It helps you to create quickly dynamic REST apis to your host server. Highly extensible and opensource framework. Your quick solution to connect your any devices with your services and get you started with real challenge that is to design the best application solving the problem.
Loopback uses yeoman to scaffold your node application which is controlled by loopback’s cli. This cli acts as a command center of the developer while building the application.

1
$slc loopback // to get started with the loopback

nodejs stack

you can give yo-ur application name over here : we will be giving it as viva-la-vida

A series of interaction would be there to help you get setup some key points in the application such as what should be the name of the directory in which the app will contain, which version of loopback would you like use, what kind of application you have in mind? This would include what database you want to choose from. Its pretty simple answers which would help the yeoman scaffold your app best to your requirement.

loopback

It will run npm install for you and install all the dependies for you.
Then comes the need to define the model of data which you require for your REST api.

1
2
3
$cd viva-la-vida // to enter into the application root directory.
$slc loopback:model

loopback

Now comes the real game, which is to design your model which will be exposed using REST api.

It asks you to choose your datasource, currently we dont have a datasource other then memory so go wth memory for now later we will be swicthing to mongodb.

You will be asked to give a name to your model followed by what base class would you like choose for your model. There are some prebuild base-class models which you could choose from and there is a custom option too which could help you design your own model.

A standard which we following while building up REST api is to give a pural name to the api endpoint to that model keeping things standard and consistent through out the application. You can also choose weather you want to keep your model common between your client and server side both or you want to keep your model only at server side. In MEAN stack it is common to use a common mode structure which could be used at client side and at server side, which saves time and keep things in consistant form.

There are multiple best practices which has been included in loopback including structure, security, permissions and access limits etc.

Now comes adding properties to the model person, its more like adding column to the table, we have defined ‘firstname’ as our first property. It will ask you to choose the type of the property to be defined for the column we have selected string as our type then it ask you whether you want to keep this property as required or not. What should be the default value for this property.

That was simple right, similarity you mention all the properties which is required in the model. So loopback has done a lot of work for us setting up our application with basic structure which is extensible to build a full-fledged application on this.

Go ahead and see the whole code in your editor and run the application by typing in:

1
$node .

Most likely you will be asked to access http://0.0.0.0:3000/explorer (http://0.0.0.0:3000/explorer) to interact with REST apis graphically unless you have given a custom path for server. Loopback has designed a great REST interacting GUI helping you to test your REST calls. You can perform PUT, POST, GET, DELETE etc.

Now time change your datasource to mongodb.

1
$slc loopback:datasource

This will give you an options of databases which are currently supported by loopback. Choose mongo for our current requirement.
You will have to install the connector for this database.
$npm install –save loopback-connector-mongodb
This will install the right conenctor for the mongodb. Following the installation change the settings of the application by updating the .loopbackrc file in the root directory with following data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"dev": {
"mongodb": {
"host": "127.0.0.1",
"database": "test",
"username": "youruser",
"password": "yourpass",
"port": 27017
}
},
"test": {
"mongodb": {
"host": "127.0.0.1",
"database": "test",
"username": "youruser",
"password": "yourpass",
"port": 27017
}
}
}

Username/password is only required if the mongodb server has authentication enabled.
Update the datasource.json file inside your server directory. With following code:

1
2
3
4
5
6
7
8
9
10
11
12
{
"mongodb": {
"host": "localhost",
"port": 27017,
"url": "http://mongodb://localhost/loopback",
"database": "mongodb",
"password": "",
"name": "mongodb",
"user": "",
"connector": "mongodb"
}
}

Now you have a datasource with the name of mongodb. You just need one more configuration to make it run. In your server folder go to model-config.json and replace db which was your default in-memory db for your model with new ‘mongodb’. This will connect your model data with your database and replicate the structure.

That’s it run you application with “node .” and experiment it as you like. This will give you a better understanding of the application.
So this was an example which involved basic REST app with nodejs and loopback. But the next step should be to make it a full-fledged node app which is monitor-able and scalable.