Node.js

SeanUdayantha
4 min readMay 15, 2022

--

Node.js is a JavaScript runtime environment that is open-source and cross-platform. It’s a widely used tool for practically any job!

Outside of the browser, Node.js operates the V8 JavaScript engine, which is at the heart of Google Chrome. As a result, Node.js is extremely fast.

Without establishing a new thread for each request, a Node.js software operates in a single process. Node.js’ standard library includes a set of asynchronous I/O primitives that prevent JavaScript code from blocking, and libraries in Node.js are often created following non-blocking paradigms, making blocking behavior the exception rather than the rule.

This allows Node.js to handle thousands of concurrent connections with a single server without adding the overhead of thread concurrency management, which may be a major cause of errors.

Because millions of frontend developers who write JavaScript for the browser can now create server-side code in addition to client-side code without having to learn a new language, Node.js offers a distinct advantage.

The new ECMAScript standards can be used without issue in Node.js because you don’t have to wait for all of your users’ browsers to update — you choose which ECMAScript version to use by changing the Node.js version, and you can also enable specific experimental features by running Node.js with flags.

The Node Ecosystem

Node is cool in and of itself, but it wouldn’t be where it is now without npm, the Node package manager. Node can be expanded using frameworks and plugins, and code can be readily installed and merged into new projects, thanks to npm. Consider the following scenario: you’re writing a script and need to send a POST request to an external resource. You may use JS’s built-in fetch function or axios, which leverages promises and is more user-friendly. If you wish to use axios in your project, you need put your script in its own folder and run:

npm init
npm install axios

This generates a package.json file that stores project settings and installed packages. The npm install program creates a subdirectory named node modules in which downloaded modules are stored. Be aware that for large projects with multiple modules, this folder might get rather huge.

To use axios in your script, you’d put this line right at the top:

const axios = require('axios');

Or, in ES6 syntax:

import axios from 'axios';

This imports the module from node modules and makes it available to your script. You may then utilize it as if it were a component of your project and a function you created.

Axios is a simple example of a handy npm tool. Other modules, like as React, Angular, and Vue, are whole web application frameworks for creating interactive programs that run in the browser. The term “Node applications” refers to apps created with these frameworks. Despite the fact that the end outcome will be static HTML that can be served with any old web server, Node is used for development and packages are installed from the Node ecosystem using npm.

After all, it’s all JavaScript. The development code will frequently employ “next-gen” JavaScript, such as ES6, and will be built with Webpack and Babel into a single large “bundle.js” file that can be delivered to the client to run.

What is the relationship between Node and nginx and Apache?

Node does not interact directly with nginx or Apache; it just executes.js files. However, Node applications developed using React or other frameworks behave differently from standard HTML websites.

You’re just presenting a blank HTML page that loads a bundle.js file with React. This bundle file works just like any other javaScript file, and it may be hosted on nginx or Apache. The bundle launches React, which subsequently renders your content on the web page. The bundle file contains all of the material.

A popular library named Express is worth mentioning. Express is a web content server that may also act as an HTTP server. Express is frequently used as the router in Node-based REST APIs; it may listen on a port, redirect the request to another function (usually accessing another resource like a database), and then provide an HTTP answer.

In this example, you’d run Express behind the nginx server that serves your static content, and all /api routes would be sent to it. This allows your web app to access external resources while also taking use of Node’s server-side scripting capabilities and database integration. For the /api route, you may use nginx as a reverse proxy, and for the other static content, you can use it as a web server.

Express, on the other hand, is not a web server. It should not be used instead of nginx in this case. It may be used as one, and it’s quite handy for setting up a basic Express server for development or a simple website with little traffic. However, it pales in comparison to the performance of native programs like nginx and Apache. If you’re creating a genuine web application, use nginx to serve it, and solely use Express to serve APIs.

--

--

No responses yet