POWERS OF NODE.JS

Fasae Oluwakemi
Devcrib
Published in
3 min readDec 8, 2017

WHAT IS NODE.JS?

React.js, Vue.js, Angular.Js, and so many other JavaScript libraries and tools keep evolving and becoming popular. It would seem JavaScript is eating up the world of technology with every new release and enhancements. However, one would think JavaScript is just about front-end technology, but it has proven it’s not,by bringing about a run-time system for creating server-side applications. In 2009, Ryan Dhal wrote Node.js as an open-source cross platform JavaScript run-time which executes JavaScript server-side codes which are used to build the back-end software of a web application.

HOW DOES NODE.js WORK?

Node.js brings about event-driven programming to web servers. In an event driven application, there is a main loop that listens for events, then triggers a callback function when one of those events is detected. It also supports asynchronous programming in which a unit of work runs separately from the application thread and notifies the calling thread of its completion, progress or failure. In traditional web-serving methods where each connection requested spawns a new thread at every point of request and uses more RAM, memory use is less efficient as opposed to using Node.js which allows and supports thousands of concurrent connections. Take serving a file for example in PHP, it sends the task to the computer’s file system, waits till the file system opens and reads the file and then returns the content to the client before it becomes ready to handle the next task. Node.js eliminates the waiting period and simply moves on to the next task and when the file system opens and reads the file, the server then return the content to the client.

NODE PACKAGE MANAGER

The npm comes by default with every Node.js installation. It contains a set of publicly available, reusable, components or codes, available through easy installation via an online repository. It also makes it easy to update the code that you’re using. These set of reusable codes are called packages or modules. A package in Node.js contains all the files you need for a module along with a file called “package.json” that contains metadata about the package. Modules are JavaScript libraries you can include in your project.A typical application, such as a website, will depend on dozens or hundreds of packages. npm is made up of three distinct pieces: the website, the registry, and the CLI. The website serves as the primary tool for users to discover packages, the registry is a large database of information about packages, and the CLI is how developers publish their packages on the registry or download packages they wish to install. Including a module is easy; simply call the require() function, like this:

var http = require('http');

GETTING STARTED WITH NODE

  • Download and install node.js from here.
  • After you have downloaded and installed, to confirm if node and npm have been installed, in the command prompt, type each of the following:

npm -v

node -v

You should see the version of node and npm installed on you computer like this.

  • Create a node.js file named “hello-World.js” and add the following code:

import http from 'http';

const server = http.createServer((req, res){

res.writeHead(200, {'Content-Type': 'text/html'});

res.end('Hello World');

}).listen(8080);

  • Save this file on your computer. This code simply tells the computer to write “Hello World” and it should be accessed on port 8080.
  • Navigate to the folder that contains the file you just saved in your command prompt.
  • To run the file you just saved, write node hello-World.js and hit enter, you should see hello world displayed on the screen like this:

Following the steps above, you have been able to run your first node application.

Node.js requires extra work because you have to do everything yourself since it is just an environment, but the payoff of a fast and robust application is worth it. To make things easier, you can always pick up a framework like express. Read about it here.

References

--

--