What Is Node?
It’s generally used for constructing functions, working with trendy internet app frameworks, and server-side scripting with JS. In observe, Node means that you can run
.js information with the
node command much like how you’ll run
.py information with
Node can be used to construct desktop apps, via the usage of a framework known as Electron. Electron powers Slack, Discord, Visible Studio Code, and Skype. Basically, it runs a slimmed down model of Chrome designed for the desktop, and the app itself is definitely only a internet app. As a consequence although, Electron apps are very RAM hungry, and can run a bit worse than their native counterparts, however that commerce is commonly made for uniformity throughout all working techniques and the online.
The Node Ecosystem (npm Modules)
Node itself is cool, nevertheless it wouldn’t be anyplace near the place it’s immediately if it weren’t for
npm, the Node bundle supervisor.
npm permits Node to be prolonged with frameworks and plugins, and permits code to be simply put in and integrated into a brand new mission.
For instance, say you’re engaged on a script and have to make a POST request to an exterior useful resource. You may use JS’s in-built
fetch technique, or you might use axios, which makes use of guarantees and is less complicated to make use of. In case you wished so as to add axios to your mission, you’d wish to transfer your script to its personal folder and run:
npm init npm set up axios
This creates a file known as
bundle.json, which retains observe of mission settings and put in packages. The
npm set up command will create a folder known as
node_modules, which shops downloaded modules. Be warned, this folder may be very giant on giant initiatives with many modules.
To make use of axios in your script, you’d put this line proper on the high:
const axios = require('axios');
Or, in ES6 syntax:
import axios from 'axios';
This imports the module from
node_modules, and permits your script to entry it. From there, you need to use it prefer it’s a part of your mission and simply one other perform you made.
Axios is only a primary instance of a helpful
npm utility. You’ve most likely heard of different modules like React, Angular, or Vue; all of those are full internet utility frameworks used for constructing interactive apps that run within the browser. Apps constructed with these frameworks are generally reffered to as “Node apps.” Whereas they’re not truly working with Node, as the ultimate product will likely be static HTML you could serve with any outdated internet server, Node is used for improvement and packages are put in from the Node ecosystem with
How Does Node Work with nginx and Apache?
Node itself doesn’t interface straight with nginx or Apache—all Node does is run
.js information. However, Node apps constructed with React or different frameworks do work a bit otherwise than regular HTML pages.
With React, you’re primarily serving a clean HTML web page that masses a
One factor to notice is a well-liked library known as Specific. Specific serves internet content material, and may perform as an HTTP server. It’s generally used because the router for making a Node-based REST API; Specific can hear on a port, ahead the request to a different perform (usually accessing one other useful resource like a database), then ship again an HTTP response.
On this case, you’d put Specific behind the nginx server you employ in your static content material, and route all
/api routes to Specific. This permits your internet app to entry exterior sources, and make full use of Node’s server facet scripting energy and skill to interface with databases. You should use nginx as a reverse proxy for the
/api route, and use it as an online server for the opposite static content material.
Nonetheless, Specific just isn’t an online server. It shouldn’t take the place of nginx on this instance. It will probably perform as one, and it’s very helpful to arrange a easy Specific server for improvement, or a easy web page that isn’t receiving a lot site visitors. But it surely’s a far cry from the efficiency of nginx and Apache, that are native apps. In case you’re constructing an actual internet utility, serve it with nginx, and use Specific solely to serve APIs.
Ought to I Use Node for My Web site?
In case you’re not constructing an online app, you most likely don’t want Node. It’s not meant for static pages. You actually can, however you gained’t see a lot profit. In case you’re planning on utilizing a full internet framework like React for a static (and even principally static) website, you may wish to rethink, as React introduces numerous overhead to your website. React loading occasions are typically a lot slower with out server facet rendering, which is okay for internet functions designed for lengthy session durations, however not for a weblog or something on the lookout for good web optimization. Vanilla JS can do fairly a little bit of DOM manipulation by itself, particularly with the assistance of jQuery.