What is Node?
The modern web applications have come a long way over the years with the introduction of many popular frameworks such as Angular, React js, Vue js and many more down the line. All these frameworks are client-based which means they revolve around the browser specificity.
But when it came to developing server-based applications there was a less or you can say kind of empty and this is where Node.js came into the picture.
Node.js is an open source, cross-platform runtime environment used for development of server-side web applications. Node.js applications are written in Javascript and can be run on a wide variety of operating systems, it's interoperable. Based on event-driven architecture and a non-blocking I/O API that is designed to optimize the scalability for real-time web apps.
Why to Use Node JS ?
- Node uses the V8 JavaScript Runtime engine which is used by Google Chrome. Node is having a wrapper over the JavaScript engine which makes the runtime engine much faster and with that it processes requests at much faster rate.
- Ability of handling concurrent request with minimal overhead over a single process.
- Asynchronous event driven architecture which basically means that if a request comes to Node for some I/O operations, it will execute the operation in background and continue with processing other requests.
- Also the most important one a wide operating community support for the Node js framework backed with the latest trends in web development.
What companies uses Node JS?
Great one
Companies using Node for building their primary application such as Netflix, Trello, Uber, Walmart, Medium, Paypal, linkedIn, E-bay, Mozilla etc
When to Use and not use Node
Code-it
Basically Node is best for using in streaming or event based real time applications like
- Gaming servers to perform millions of request at at time.
- Chat applications.
- Collaborative environment such as Trello.
- Document management systems.
- Streaming servers such as Netflix to pull multimedia contents from the server.
- Advertisement servers as thousands of request to pull advertisements from a server and Node is best to handle this.
Node is best when you need a high level of concurrency and Asynchronous event drive I/O operations with in a less amount of dedicated CPU Time.
The only known scenario where node is not required, if there are some long running calculations in the background which requires a lot of processing time as Node is structured to be single threaded .
What are modules?
Single unit
Modules in Node JS are a way of encapsulating code in a separate logical unit. The main purpose of modules is that it will help to make your code more manageable and maintainable for future use. Each module is an independent entity with their own encapsulated functionality as it will be managed as a separate unit of work.
Popular modules in Node
- Express
- Socket.io
- Async.js
- Browserify
- Request
- Grunt
- Koa.js
- Underscore.js
- Mocha
- Karma
- Passport
- Cheerio
- Jade
- Morgan
- Restify
and many more……
Creating and Publishing Custom NPM Modules
Node has the ability to create custom modules and allows to include those custom modules in your app.
Let’s see how to create custom one
Step 1 :- Create a Multiple.js and write down
var exports = module.exports = {};
exports.MultipleNumber = function(a,b) {
return a\*b;
}
Now we have created our custom module which has the functionality of multiplying of two numbers. Its time to create an application which will call this module.
Step 2: Create a file called app.js which is the main entry point
var Mulitple = require('./Multiple.js');
console.log(Mulitple.MultipleNumber(78,32));
As we are not using the NPM as of now to install our Multiple.js module. So to do that we need to publish a module on the internet.
Publishing NPM custom modules
publish
To publish means this will be available to internet and any one install it and start using it.
Steps
- Create a repo on Github(online code repo management).
- Now its time to tell NPM that who is author of this custom module, what email id, company url, metadata, all this information is needed when you are going to publish your own custom modules to NPM.
npm set init.author.name "some-name" npm set init.author.email "some@email.com" npm set init.author.url 'your url if any'
3. This step will require to login into the npm using the credentials provided in the last step.
npm login
4. Initialise your package which means to create a package.json file.
npm init
It will ask for many question when you run this command. Answer them wisely.
5. Publishing to Github- Now is the time to publish your source files to Github
git add .
git commit -m "Initial code release"
git tag v0.0.1
git push origin master --tags
6. Publish to NPM- The final step is to publish your module into the npm registry.
npm publish
Now this actual step will publish your module to the NPM registry which makes it available to worldwide internet. Any one can use it now.
Hope you like this information, Please comment and share
I will publish further more articles related to Node internals, how V8 works in node, Background internals of Libuv project.
Keep sharing and clap.