What is the knot?
It is commonly used for building applications, working with modern web application frameworks, and creating server-side scripts with JS. In practice, Node allows you to run
.js files with the
node command similar to how you would run
.py files with
Node is also used to create desktop applications, through the use of a framework called Electron. Electron powers Slack, Discord, Visual Studio Code, and Skype. Essentially, it’s running a lite version of Chrome designed for the desktop, and the app itself is actually just a web app. As a result, Electron apps are very RAM hungry and will perform a little worse than their native counterparts, but this commerce is often done for consistency across all operating systems and across the web.
The node ecosystem (npm modules)
The knot itself is cool, but it wouldn’t be near where it is today if it weren’t for
npm, the Node package manager.
npm allows Node to be extended with frameworks and plugins, and allows code to be easily installed and incorporated into a new project.
For example, suppose you are working on a script and need to send a POST request to an external resource. You can use the built-in JS
fetch method, or you can use axios, which uses promises and is easier to use. If you wanted to add axios to your project, you would want to move your script to its own folder and run:
npm init npm install axios
This creates a file called
package.json, which keeps track of project settings and installed packages. The
npm install The command will create a folder called
node_modules, which stores downloaded modules. Be careful, this folder can be very large on large projects with many modules.
To use axios in your script, you put this line at the top:
const axios = require('axios');
Or, in ES6 syntax:
import axios from 'axios';
This imports the module from
node_modules, and allows your script to access it. From there, you can use it as if it’s part of your project and just another function that you created.
Axios is just a basic example of a
npm utility. You’ve probably heard of other modules like React, Angular, or Vue; they are all complete web application frameworks used to build interactive applications that run in the browser. Applications created with these frameworks are commonly referred to as “node applications”. While they don’t actually work with Node, as the end product will be static HTML that you can use with any old web server, Node is used for development and packages are installed from the Node ecosystem. with
How does Node work with nginx and Apache?
Node itself does not interface directly with nginx or Apache – whatever Node does is executed
.js files. But, Node apps built with React or other frameworks work a little differently than normal HTML pages.
With React, you’re essentially serving up a blank HTML page that loads a
One thing to note is a popular library called Express. Express serves web content and can function as an HTTP server. It is commonly used as a router to create a node-based REST API; Express can listen on a port, pass the request to another function (usually by accessing another resource such as a database), and then return an HTTP response.
In this case, you would put Express behind the nginx server you use for your static content and route all
/api the routes to the Express. This allows your web application to access external resources and make full use of Node’s server-side scripting power and ability to interface with databases. You can use nginx as a reverse proxy for the
/api route and use it as a web server for other static content.
However, Express is not a web server. It should not replace nginx in this example. It can work as one, and it’s very useful to set up a simple Express server for development, or a simple page that doesn’t get a lot of traffic. But we are far from the performance of nginx and Apache, which are native applications. If you are building a real web application, serve it with nginx and use Express only to serve APIs.
Should I be using Node for my website?
If you aren’t building a web app, you probably don’t need Node. It is not intended for static pages. You certainly can, but you won’t see much benefit. If you plan on using a full web framework like React for a static (or even mostly static) site, you might want to reconsider, as React introduces a lot of overhead to your site. React load times are generally much slower without server-side rendering, which is good for web apps designed for long session durations, but not for a blog or anything looking for good SEO. Vanilla JS can do quite a bit of DOM manipulation on its own, especially with the help of jQuery.