logo

Node.js - Overview

What is Node.js

Node.js is a JavaScript runtime, which means it can parse and execute javascript code:

$ node
> var a = 1;
undefined
> a + 1
2

It is built on V8, the JavaScript engine for Chrome, it shares the same features that run in the browser. And Node adds a few APIs beyond V8.

Node doesn’t run any of your application code in parallel

Pros and Cons

Pros:

  • same language (JavaScript) for both server and client side.
  • event-driven, non-blocking I/O, fast and lightweight.
  • npm, the package manager.
  • awesome libraries.

Cons:

  • not supporting multi-threading.
  • not suitable for heavy computing problems.
  • no strict checking, could result in runtime problems.

When to use nodejs

Data-driven, I/O-driven, event-driven, and non-blocking applications benefit the best from a Node.js implementation.

  • Data-intensive programs are focus on retrieving and storing data,
  • I/O-intensive programs focus on interacting with external resources.

Web servers are a perfect use case that benefits from Node’s inherent features.

When not to use Node.js

Node a bad choice for CPU-intensive programs.

Check Versions And Upgrade

Check version of node:

$ node -v
vX.X.X

$ nvm current

Check version of npm:

$ npm -v
X.X.X

Install latest LTS version

$ nvm install --lts

Get Project Folder

process.cwd();

Development and Deployment

nodemon

nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.

Install nodemon:

$ sudo npm install -g nodemon

pm2

http://pm2.keymetrics.io/

Install pm2:

$ sudo npm install -g pm2

Start production, with max memory:

$ NODE_ENV=production pm2 -f start app.js --max-memory-restart 200M

Monitor

$ pm2 monit