What is Node.JS and what is it used for? – CloudSavvy IT

.JS node

If you do web development, you will probably have to learn and write JavaScript at some point. Node is intended to simplify web application development by unifying server-side and client-side languages.

What is the knot?

Node.JS, commonly referred to as just 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 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 python:

node main.js

JavaScript code works a little differently when executed in Node. First of all, there is no DOM (the HTML loaded from a webpage), so you can’t access HTML elements or use a library like jQuery with much effect (although there is 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 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 npm.

It’s all JavaScript, after all. Often the development code will use “next generation” JavaScript like ES6, and will be compiled with Webpack and Babel into a large “bundle.js” file that can be provided to the client to run.

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 bundle.js to file. This bundled 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 go to the webpage and display your content. All 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 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.

    Use nginx 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?

Are you building a web application? If so, you should seriously consider Node. The alternatives would be Ruby on Rails, Laravel with PHP and Django with Python. But Node has by far the largest community and has a lot of choices when it comes to frameworks. If you don’t like Ruby on Rails, you’re stuck with it without learning another language, but switching from a framework like React to Vue is a lot easier, since they’re both JavaScript.

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.

Leave a Comment