Tuesday, September 8, 2015

NodeJS Server Uptime

In NodeJS, everything is handled by a single javascript thread. So if an error get occurred, the server will stop.But this won't happen in traditional web servers. Because, in traditional web servers, there is a thread pool to handle requests. For each request, a thread will be assigned. If there is an error, that particular thread will discarded and new thread will be allocated.
In the following code, there are some invalid code.
In the first request, error get occurred and then server won't process any more requests.
Using forever we can automatically restart NodeJS server. First install forever like below.
Now, run above code using following command.
In the first client request server will crash. But forever will restart the server again. Forever is not a better way to handle errors in NodeJS application. But this is useful as a back for the unexpected.

Measuring Executing Time In Javascript

In this post I will explain how to use console.time() for measuring time in javascript program.
We can write a function like below to verify the execution time for a particular program.
But using console.time(), we can write our code more clearly like below.