If you’re doing any sort of web development, you’re probably going to have to learn and write JavaScript at some point. Node is meant to simplify web application development by unifying the server and client side languages.
What Is Node?
Node.JS, commonly referred to simply as Node, is a JavaScript runtime that allows you to run JS code outside of a web browser. Node is built on Google’s V8 JavaScript engine, the same one used for processing in Chrome.
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 python:
JavaScript code works a bit different when running in Node. First of all, there’s no DOM (the loaded HTML of a webpage), so you can’t access HTML elements or use a library like jQuery to much effect (though there are alternatives). But, you have access to system resources, and can read and write files, make network connections, and even serve applications.
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:
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:
Or, in ES6 syntax:
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 npm.
It’s all JavaScript, after all. Often, the development code will make use of “next-gen” JavaScript like ES6, and be compiled down with Webpack and Babel into one big “bundle.js” file that can be served to the client to run.
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 bundle.js file. This bundle file works like any other javaScript file, and you can use nginx or Apache as a web server to host it. The bundle loads React, which will then render itself onto the web page and display your content. All the content is contained in the bundle file.
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?
Are you building a web app? If so, then you should seriously consider Node. Alternatives would be Ruby on Rails, Laravel with PHP, and Django with Python. But Node has the biggest community by far, and has a lot of choice when it comes to frameworks. If you don’t like Ruby on Rails, you’re stuck with it without learning another language, but the shift from a framework like React to Vue is much simpler, as they’re both JavaScript.
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.