قالب وردپرس درنا توس
Home / Tips and Tricks / What is Node.JS and what is it used for? – CloudSavvy IT

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



Node .JS

If you are doing some form of web development, you will probably need to learn and write JavaScript at some point. The node is intended to simplify the development of web applications by combining the server and client side languages.

What is a node?

Node.JS, often called Node, is a JavaScript runtime that allows you to run JS code outside of a web browser. The node is built on Google̵

7;s V8 JavaScript engine, the same one used for processing in Chrome.

It is commonly used to build applications, work with modern web app frameworks and script pages on the server side with JS. In practice, Node allows you to drive .js files with node command similar to how you would drive .py files with python:

node main.js

JavaScript code works a little differently when you run Node. First of all, there is no DOM (the loaded HTML on a web page), so you can not access HTML elements or use a library like jQuery to great effect (although there are options). But you have access to system resources and can read and write files, create network connections and even operate applications.

The node is also used to build desktop apps, using a framework called Electron. Electron powers Slack, Discord, Visual Studio Code and Skype. Basically, it runs a slim version of Chrome designed for the desktop, and the app itself is really just a web app. As a result, the Electron apps are very RAM-hungry and will work a little worse than their original counterparts, but that trade is often done for consistency across all operating systems and the web.

Node Ecosystem (npm modules)

The node itself is cool, but it would not be anywhere near it is today if it were not for npm, Node packet manager. npm makes it possible to extend Node with frames and plugins, and makes it easy to install and integrate code in a new project.

For example, say you are working on a script and need to send a POST request to an external resource. You can use JS’s built-in fetch or you can use axios, which use promises and are easier to use. If you wanted to add axios to your project, move your script to its own folder and run:

npm init
npm install axios

This creates a file named package.json, which keeps track of project settings and installed packages. The npm install command creates a folder named node_modules, which stores downloaded modules. Warning, this folder can be very large on large projects with many modules.

To use axios in your script, add 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 let your script access it. From there, you can use it as if it were part of your project and just another function you did.

Axios is just a basic example of a useful npm tool. You have probably heard of other modules such as React, Angular or Vue; all of these are complete web application frameworks used to build interactive apps running in the browser. Apps built with these frameworks are commonly referred to as “Node apps.” While they do not actually run 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 JavaScript. Often the development code will use “next-gen” JavaScript such as ES6, and will be compiled with Webpack and Babel into a large “bundle.js” file that can be served to the client to run.

How does the node work with nginx and Apache?

The Node itself does not border directly on nginx or Apache – everything Node does runs .js files. But Node apps built with React or other frameworks work a little differently than regular HTML pages.

With React, you basically serve a blank HTML page that loads one bundle.js file. This package file works like any other javaScript file, and you can use nginx or Apache as a web server to host it. The package loads React, which will then be displayed on the web page and display your content. All content is in the package file.

One thing to note is a popular library called Express. Express serves web content and can act as an HTTP server. It is commonly used as a router to create a node-based REST API; Express can listen on a port, forward the request to another function (usually accessing another resource such as a database) and then send back an HTTP response.

In this case, you would 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 take advantage of Node’s full server-side scripting power and the ability to interface with databases. You can use nginx as a reverse proxy for /api route, and use it as a web server for the other static content.

    Use nginx as a web server for other static content.

In all cases, Express is not a web server. Nginx should not be included in this example. It can work as one, and it is very useful to configure a simple Express server for development, or a simple page that does not get much traffic. But it is far from the performance of nginx and Apache, which are built-in apps. If you are building a real web application, serve it with nginx and use Express just to display APIs.

Should I use the node for my site?

Are you building a web app? In that case, you should seriously consider Node. Options would be Ruby on Rails, Laravel with PHP and Django with Python. But Node has the largest community by far and has many choices when it comes to frameworks. If you do not like Ruby on Rails, you are stuck with it without learning another language, but the transition from a framework like React to Vue is much easier, because they are both JavaScript.

If you are not building a web app, you probably do not need Node. It is not intended for static pages. You for sure Can, but you will not see much benefit. If you are planning to use a complete web framework like React for a static (or even mostly static) website, you may want to consider again, as React introduces a lot of overhead on your site. React loading times are generally much slower without server-side rendering, which is good for web applications designed for long session times, but not for a blog or anything looking for good SEO. Vanilla JS can do a lot of DOM manipulation on its own, especially using jQuery.


Source link