What is Node js?
Node.js is one of the most widely used technologies today to build API’s that power client end applications. Before this powerful technology hit the computers of developers worldwide, the world of web applications looked very different. In this beginner’s guide I’ll go over why the world needed Node.js, what it does, and how we can use it to build a simple web server.
Traditional server side technologies, for example, a PHP app ran on an Apache server, work asynchronously, waiting for one process to finish before starting on the next. This is known as blocking, or synchronous architecture. When a request from a client iis received on the server, a thread is allocated to handle that request. That thread can’t be used to serve another client until it has completed it’s database query, or whatever query it is tasked with. Every new request creates a new thread, inevitably leading to all available threads being exhausted, and the client having to wait for a thread to become available.
Node.js, on the other hand, uses non-blocking I/O calls which allow it to sort tens of thousands of simultaneous requests which are held on a single thread, forming what’s called an event loop. Let’s explore what this event loop looks like.
Since Node.js is asynchronous by default, a single thread is used to handle all incoming requests. Let’s say we recieve a request from a client, and we must query a database. If other requests come in, we don’t need to wait for a thread to become available, we can process those requests concurrently. After the database returns with a result of the first request query, it gives an event to the event queue, which is continually being scanned by Node.js in the background. Events that appear in the event queue are immediately pulled and processed. This architecture optimizes scalability in web applications and makes Node.js extremely fast and efficient.
So, what can we build with Node.js?
Node.js is utilized extensively for RESTful APIs. It’s ideal if your application requires the ability to get data from a database and then serve it to a client; for this reason it’s widely used in real time services such as chat applications and games. It shouldn’t be used for CPU intensive applications that don’t operate extensively with file systems or the network.
Cool! How do I install?
Installing Node.js is as simple as heading to the official website, nodejs.org, and downloading one of the pre-built installers for Windows, Mac OS, or Linux.
What about NPM?
NPM is a vital component of Node.js. It stands for Node Package Manager (or sometimes Node Package Modules), and it functions to install and manage node modules or programs. It makes it quick and easy to specify and link dependencies. Modules get installed into the “node_modules” folder.
So if we run npm install express, that installs the express module into the node modules folder, and if we don’t have a node modules folder, it’ll create one for us. You can also install modules globally by accessing the -g flag, and that will install it on you system so that you can access it anywhere and not just in that directory or application.
Some popular modules include:
- Moment: parse, manipulate and display dates
- Express: web development framework
- Reactdom: React package for working with the DOM
- Mongo/ Mongoose: Object Data Modeling (ODM) library for MongoDB and Node. js
One last thing: Package.json
The package.json is a super important file used in every node applications or package. It resides in the root of your directory and communicates vital information to npm, such as how your package is structured and what it must do to install it. When you run npm init in your terminal, npm will ask you to input some data about your program and create your package.json file for you, or you can set it manually.