NodeJS

4 min. read

NodeJs is a Runtime Environment

Introduction

NodeJS is a JavaScript runtime environment, which runs on Google’s V8, mostly for running JavaScript on the backend server. Technically NodeJs is a wrapper around the Google V8 Virtual Machine.

NodeJs uses non-blocking or Asynchronous API’s

Single Threaded using an Event Queue, Node continuously monitors this queue, if the event is ready, it takes it out and processes it.

Node is ideal for IO based process (like file access, database access)

Node is not ideal for CPU intensive features like video encoding.

NodeJs

In node the global object is global
Global Objects https://nodejs.org/api/globals.html
Process Object https://nodejs.org/api/process.html

Microservices
https://medium.com/@cramirez92/build-a-nodejs-cinema-microservice-and-deploying-it-with-docker-part-1-7e28e25bfa8b

Frameworks:
http://nodeframework.com/

RESTful API development:
http://apiworkbench.com/docs/

Installing Node

https://nodejs.org/en/

Or use Node Version Manager
https://github.com/nvm-sh/nvm

Packages

NPM - Node Package Manager
Uses Semantic Versioning (SemVer)

~1.2.3 is the same as 1.2.X
^1.2.3 is the same as 1.X.X

https://semver.npmjs.com/

NVM - Node Version Manager

Built-in Modules

File-system (fs)
Http
Crypto
Zip

In Node, window is global, for example, global.console.log();

Variables are added to the global scope

Os
Fs
Events
Http

Timer Functions

Global objects:

setTimeout()
clearTimeout()
setInterval()
clearInterval()

Modules

Every file in Node is considered a module

Variables and functions are scoped to the module, thus private by default.

You can load modules in a few different ways.

Common NodeJS Module:

1
const http = require(‘http’);

ECMAScript Module:

Rename file to .mjs, then:

1
import http from ‘http’;

Or, using named imports, for example:

1
import { createServer } from ‘http’;

Modes

You can run NodeJS is a few different modes.

REPL Mode

Read
Eval
Print
Loop

Start by typing:

1
node

In a terminal window

Common REPL commands

.help
.break
.clear
.editor
.load
.save
.exit (or CTRL + D)

Double tap TAB, to show more information about global, and/or objects.

For example, pressing double tab on an empty line, will return:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Array                 ArrayBuffer           Atomics
BigInt BigInt64Array BigUint64Array
Boolean Buffer DataView
Date Error EvalError
FinalizationRegistry Float32Array Float64Array
Function Infinity Int16Array
Int32Array Int8Array Intl
JSON Map Math
NaN Number Object
Promise Proxy RangeError
ReferenceError Reflect RegExp
Set SharedArrayBuffer String
Symbol SyntaxError TextDecoder
TextEncoder TypeError URIError
URL URLSearchParams Uint16Array
Uint32Array Uint8Array Uint8ClampedArray
WeakMap WeakRef WeakSet
WebAssembly _ _error
assert async_hooks buffer
child_process clearImmediate clearInterval
clearTimeout cluster console
constants crypto decodeURI
decodeURIComponent dgram dns
domain encodeURI encodeURIComponent
escape eval events
fs global globalThis
http http2 https
inspector isFinite isNaN
module net os
parseFloat parseInt path
perf_hooks process punycode
querystring queueMicrotask readline
repl require setImmediate
setInterval setTimeout stream
string_decoder sys timers
tls trace_events tty
undefined unescape url
util v8 vm
wasi worker_threads zlib

__defineGetter__ __defineSetter__ __lookupGetter__
__lookupSetter__ __proto__ hasOwnProperty
isPrototypeOf propertyIsEnumerable toLocaleString
toString valueOf

constructor

CLI Mode

1
node

Code Snippets

HTTP Web Server

If you need to create a simple web-server, without using ExpressJS, create a new script, with content:

1
2
3
4
5
6
7
8
9
const http = require(‘http’);

const server = http.createServer((req, res) => {
res.end(‘CONTENT’);
});

server.listen(3000,()=>{
console.log(‘Server Running on port 3000’);
});

Deployment

NodeJS can be deployed to:

Functions-as-a-Service

Firebase Functions
AWS Lamba

Server

Heroku
AWS
GCP
Azure

Courses

https://nodejs.dev/learn/introduction-to-nodejs

Working With Node.js

Node.js: Getting Started

Udemy Courses

https://academy.zenva.com/course/node-js-and-express-for-game-development/

References

Global Objects
Process Object
Microservices
Frameworks
RESTful API development
Hosting
NodeJS is not a framework
Why Node
Samer Buna