Understanding Node.js

Richard Chea
1 min readDec 7, 2020

What is Node?

Node is an asynchronous run-time JavaScript environment built on top of Chrome. It’s designed to build scalable network applications such as a web app, real-time chat app, REST API server, etc. Node is often used to build back-end services like APIs.

Sample code and explanation.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

This sample shows a server that responds with ‘Hello World’ if you connect to it. By using the createServer method to create a server and then the server.listen function to listen on a specific IP address and respond to connections with that message.

Advantages?

Node is great for prototyping and agile development. It’s capable of building superfast and highly scalable services. JavaScript is the language it’s written in and allows front-end developers to use their coding ability on the back-end. You’ll also be able to use the same naming convention, tools, and best practices as JavaScript.

How do Events work?

The runtime initiates an event loop and it allows Node to push intensive operations off to a separate thread so only very fast non-blocking operations happen on the main thread.

How to install?

Depending on your OS you’ll have different commands to run.

Check out the URL: https://nodejs.org/en/download/package-manager/

--

--