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
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
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 | Array ArrayBuffer Atomics |
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 | const http = require(‘http’); |
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
Paid Pathways
Paid 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