What is Node Js?
How can you execute a JavaScript file outside the browser?
Node js is a runtime built on top of Chrome's V8. It allows you to develop apps in JavaScript outside of the browser. It's single-threaded non blocking and asynchronous. This is achieved by the use of an event loop at the core of Node js.
Application of Node js
With the help of Node js, you can run JavaScript outside the browser, which can be used for pretty much everything!
API and Servers
CLI's
Scripting
Automation and bots
Installation
For Windows User
You can install using the official windows installer or the windows package manager Chocolatey.
For Linux/macOS User
You can install the node with NVM (node version manager )
Executing Node Js
Using REPL
Using File
Global Objects
Common Objects
__dirname
__filename
process
exports, module, and require
Rest Objects
- you can check all other global Objects on the official website
Modules
How can you share your JavaScript with another JavaScript in your application?
Overview
A module is a reusable chunk of code that has its context. That way modules can't interfere with or pollute the global scope.
You can think of them like lego blocks that you can create, import, and share.
CommanJs, ES6 modules and Internal Moudules
By default, Node.js uses common js modules. With newer versions of Node.js, we can now take advantage of ES6 modules. To opt into using this syntax, you can use the
.mjs
extension instead of.js
.
ES6 Module
Multiple named exports
//------ lib.js ------ export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } //------ main.js ------ import { square, diag } from 'lib'; console.log(square(11)); // 121 console.log(diag(4, 3)); // 5 You can also import the complete module: //------ main.js ------ import * as lib from 'lib'; console.log(lib.square(11)); // 121
Single default exports
//There can be a single default export. For example, a function: //------ myFunc.js ------ export default function () { ··· } // no semicolon! //------ main1.js ------ import myFunc from 'myFunc'; myFunc(); //Or a class: //------ MyClass.js ------ export default class { ··· } // no semicolon! //------ main2.js ------ import MyClass from 'MyClass'; const inst = new MyClass(); //Note that there is no semicolon at the end if you default-export a function or a class (which are anonymous declarations).
Common Js module
At a high level, the common js module syntax contains two primary parts:
a free variable named
exports
which contains the objects a module wishes to make available to other modules anda
require
a function that modules can use to import the exports of other modules.
Internal Module
fs
path
child
HTTP
File System
Reading a File
Writing a File
Packages
The most beautiful part about Node.js is not the JavaScript, it's the thriving community. There are millions of node projects ready to be installed and consumed by your application. These projects are called packages. A package can have several modules and other packages. Node.js has built in support for these packages so you can take advantage of them at any time.
NPM
To consume a package, we must first turn our app into a package. We can do this with a simple file called
package.json
on the root of our app. Writing it by hand is cool, but using a CLI callednpm
is better. NPM was already installed when you installed Node js.
Finding and installing a package
Most modules are hosted on a registry somewhere. The biggest and most used one is, well, the NPM registry. They don't stand alone though. Github (which owns NPM now) also allows devs to publish packages to their registry. And there are many others. The sweet thing about NPM, is that you can point to any registry, default being NPM.
Server
Node.js has access to OS level functionality, like networking tools. This allows us to build very capable servers. Mixed with the fact that Node.js is single threaded and runs an even loop for async tasks, Node.js is widely used for API's that need to respond fast and don't require heavy CPU intensive work.
Using HTTP
Using Express JS