What Is Node?
It’s commonly used for building applications, working with modern web app frameworks, and server-side scripting 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 build desktop apps, through the use of a framework called Electron. Electron powers Slack, Discord, Visual Studio Code, and Skype. Essentially, it runs a slimmed down version of Chrome designed for the desktop, and the app itself is actually just a web app. As a consequence though, Electron apps are very RAM hungry, and will run a bit worse than their native counterparts, but that trade is often made for uniformity across all operating systems and the web.
The Node Ecosystem (npm Modules)
Node itself is cool, but it wouldn’t be anywhere close to 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, say you’re working on a script and need to make a POST request to an external resource. You could use JS’s built in
fetch method, or you could use axios, which uses promises and is easier to use. If you wanted to add axios to your project, you’d 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 command will create a folder called
node_modules, which stores downloaded modules. Be warned, this folder can be very large on large projects with many modules.
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 allows your script to access it. From there, you can use it like it’s part of your project and just another function you made.
Axios is just a basic example of a useful
npm utility. You’ve probably heard of other modules like React, Angular, or Vue; all of these are full web application frameworks used for building interactive apps that run in the browser. Apps built with these frameworks are commonly reffered to as “Node apps.” While they’re not actually running with Node, as the final product will be static HTML that you can serve 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 doesn’t interface directly with nginx or Apache—all Node does is run
.js files. But, Node apps built with React or other frameworks do work a bit differently than normal HTML pages.
With React, you’re essentially serving 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’s commonly used as the router for making a Node-based REST API; Express can listen on a port, forward the request to another function (typically accessing another resource like a database), then send back an HTTP response.
In this case, you’d put Express behind the nginx server you use for your static content, and route all
/api routes to Express. This allows your web app 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 the other static content.
However, Express is not a web server. It should not take the place of nginx in this example. It can function as one, and it’s very useful to set up a simple Express server for development, or a simple page that isn’t receiving much traffic. But it’s a far cry from the performance of nginx and Apache, which are native apps. If you’re building a real web application, serve it with nginx, and use Express only to serve APIs.
Should I Use Node for My Website?
If you’re not building a web app, you probably don’t need Node. It’s not meant for static pages. You certainly can, but you won’t see much benefit. If you’re planning 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 loading times are generally much slower without server side rendering, which is fine for web applications 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.
- › “Atari Was Very, Very Hard” Nolan Bushnell on Atari, 50 Years Later
- › T-Mobile Is Selling Your App Activity: Here’s How to Opt Out
- › How Much Does It Cost to Recharge a Battery?
- › NZXT Signal 4K30 Capture Card Review: Lossless High-Quality Footage
- › A World Without Wires: 25 Years of Wi-Fi
- › The 10 Best Netflix Original Movies in 2022