Node.js
What is Node.js?
Node.js is a free and open source server environment. That is, Node.js brings JavaScript to the server and guess what it is platform independent. Node.js lets developers use JavaScript to write command line tools and for server-side scripting to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" unifying web application development around a single programming language, rather than different languages for server and client. Node.js was implemented by Rian Dahl in 2009 and combines the following,
- Google Chrome's V8 engine
- An event loop
- Package management (npm)
Chrome V8 Engine
- This compiles JavaScript directly to native machine code before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem. The compiled code is additionally optimized (and re-optimized) dynamically at runtime, based on heuristics of the code's execution profile. Optimization techniques used include inlining, elision of expensive runtime properties, and inline caching. The garbage collector is a generational incremental collector.
An event loop
- Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur. In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected.
- Although events look quite similar to callbacks, the difference lies in the fact that callback functions are called when an asynchronous function returns its result, whereas event handling works on the observer pattern. The functions that listen to events act as Observers. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which are used to bind events and event-listeners as follows
// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
// Bind event and event handler as follows
eventEmitter.on('eventName', eventHandler);
// Fire an event
eventEmitter.emit('eventName');
Node Package Manager(npm)
- It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry
Features
Single Programming Language
- If developer using other languages like asp.Net they should know other languages like VB.Net to write server-side scripts. But Node.js uses JavaScript for both server-side programming and front-end programming. It makes Node.js as a simple and robust framework. JavaScript is a common language among web developers, so web developer from any other web technology can easily adopt with node Node.js
Application Speed
- Node.js is uses JavaScript run-time, that is powered by V8, developed by Google. Generally, V8 can execute and compile JavaScript faster than other languages. Another main thing, Node.js performing all I/O operations in an asynchronous manner which consumes a lot of memory.
Lightweight
- Node.js based on event-driven architecture so every single operation and call in a script execute in asynchronous manner. This enables Node js to run on a single thread unlike other web technologies. It makes Node.js light-in-weight and It’s a main reason which makes Node Js as a most popular once.
Object Databases
- Traditional SQL databases are based on relational model which uses tables. But Node.js applications use object databases like MongoDB. It uses document-based model that uses objects resembling JSON instead of tables
Maintenance
- Applications build by other languages are less adaptive when additional scripts added for new requirements. Developers need to make changes deep inside the code. But Node.Js has a bunch of small applications instead of a single and large application. This features allow developers to add a new functionality with much ease, fast and no need to make changes deep inside the code.
Host Anywhere
- Due to its simplicity lot of developers using Node.js and it outshines other web applications. Almost all web servers and cloud-based hosting providers like Amazon , Google, Microsoft Azure and others support hosting of Node.js web applications In recent years lot of corporate giants use Node.js. A global networking site LinkedIn moved from Ruby to Node to handle their mobile traffic. It reduced the number of servers 30 to 3, i.e. nearly 90% reduction in server usage and this system was up to 20 times faster. All these figures clearly indicates , Node.js perform much faster than other technologies
Drawbacks
Unstable API
- One of the key problems that most of the developers encounter is the Application Programming Interface (API) keeps on changing at frequent intervals and does not remain stable.
- At times, a new API appears having a number of backwards-incompatible changes. As a result the developers are forced to make changes in the accessible code bases to match the compatibility with the latest version of the Node.js API.
Asynchronous Programming model
- If you want to make the applications more scale-able, the necessary requisite is adoption of the asynchronous programming model. However, many developers may find this programming model to be more difficult in comparison to the linear blocking I/O programming.
- Another con of the asynchronous programming is the codes tend to become clumsy and the programmers have to depend on the nested calls.
No Strong library support system
- The JavaScript does not have a well equipped and robust library system in comparison to other programming languages. The result is that the users are forced to take the support of common library for executing various tasks such as Object-Relational Mapping (ORM), processing of the images, handling database operations, and XML parsing etc.
- This makes it difficult for the developers to even implement the common programming tasks using Node.js.
Basic syntax
When creating a Node.js project, the npm demands the creation of index.js which contains the code to create and start the server. The below code snippet is a sample of a index.js file I created during one of the projects
const express = require('express');
const bodyParser=require('body-parser');
const mongoose=require('mongoose');
//set up express app
const app = express();
//connect to db
mongoose.connect('mongodb://localhost/ninjago');
mongoose.Promise=global.Promise;
app.use(bodyParser.json());
//initialize routes
app.use('/api',require('./routes/api'));
//error handling middleware
app.use(function(err,req,res,next){
//console.log(err);
res.status(422).send({error:err.message});
});
//listen for request
app.listen(process.env.port || 4000,function(){
console.log('Now listening for requests!');
});
That's all for this post.
Your comments are welcome!
Comments
Post a Comment