WarsawJS Slides: Template

We talk about JavaScript. Each month in Warsaw, Poland.

Speaker

Artur Siery

Node Like A Pro

2016-06-08

@dotintegral

Motivation

I found myself doing all those repetetive actions and facing the same problems with Node for years.

Turns out, most of them are easly solved by existing tools.

I didn't know that such tools exists... So I haven't search for them.

The tools

  • nodemon
  • node-inspector & iron-node
  • nvm
  • pm2
  • http-proxy (some code, yay!)

nodemon

Creating new project

  • Git init, npm install ...
  • Open Terminal Emulator / Console
  • Open IDE
  • Start typing

I had a dream

If only there was a hot reloading module for any node script...

nodemon

  • nodemon works like an alternative node launcher
  • it monitors changes made to directories and automatically restarts your app

nodemon usage

$ npm install -g nodemon

// $ node myapp.js
$ nodemon myapp.js

nodemon usage

// run with arguments
$ nodemon myapp.js --port 80

// ignore files or directories, that can change
$ nodemon --ignore tests/ myapp.js

// watch only specyfic directories
$ nodemon --watch app/ --watch node_modules/ myapp.js

nodemon usage

// run non-node scripts
$ nodemon --exec "python -v" ./myapp.py

// delay the restarts by 3 seconds
$ nodemon --delay 3 myapp.js

node-inspector
&
iron-node

How to debug node application?

  • console.log anyone?
  • Where are the Chrome's DevTools (or Firefox's, or Edge's...)

Well... here they are

Simple usage

$ npm install -g node-inspector // duh...

$ node-debug myapp.js
Automatycally launches your browser with the inspector app.

Cool features

  • All of Chrome's DevTools features!
  • Uses WebSockets for communication
  • Remote debugging
  • Live edit of running code with option to save on file system

Advanced stuff

// create web-inspector server
$ node-inspector
// use node's native debug protocol to connect
$ node --debug myapp.js

// save live edit
$ node-debug --save-live-edit myapp.js

iron-node

  • Basically the same... but a bit different
  • It's more of a standalone, desktop debugger
  • Comes with it's own version of node
  • Themable

iron-node

nvm

Backwards compability

  • In theory, Node's API is backward compatible... at least most of it
  • What to do when working on project that requires diffrent version of node?
    • Uninstall
    • Download
    • Install

nvm

NVM stands for Node Version Manager
Allows for easy version change

Global usage

// install any version
$ nvm install 0.12.9

// switch to that version
$ nvm use 0.12.9

Local usage

// run your app in different version
$ nvm run 0.12.9 myapp.js

// run shell command with different node version
$ nvm exec 0.12.9 node myapp.js

Issues

Some other node launcher-like programms (such as nodemon or node-inspector) sometimes don't get along with nvm.

pm2

Lets release our app to production

Alright, our app is ready to be deployed on production enviroment.
We run it, everything is working.
After a few hours...

crash!

What can we do?

  • Restart the app!
  • Debug
  • Deploy hotfix
  • Test more
  • Add more try and catch statements
  • Hire an QA
  • ...

... or use PM2

  • PM2 is node process manager
  • Will automatically restart any application that crashed
  • Can launch multiple instances of a single app and seamlesly route the traffic (app needs to be stateless)
  • Can restart instance when exceeds given memory limit
  • Can log STDOUT and STDERR to files

Usage

// run your app
$ pm2 start myapp.js

// run in 5 instances
$ pm2 start myapp.js -i 5

// restart after exceeding 1GB
$ pm2 start myapp.js --max-memory-restart 1024M

See what's going on

pm2 list

Realtime monitoring

pm2 monit

More features

  • Watch directory and restart on changes (like nodemon)
  • Programmatic API

node-proxy

Multiple applications

  • We can develop application
  • We can deploy it to production
  • But what about deploying another app?
    • Only one port 80!

The usual solution

  • Run applications on different ports
  • Or maybe use Docker containers
  • Routing?
    • Apache
    • Nginx
    • ...

For the pure sake of

http-proxy

  • Allows to proxy requests
  • Supports both HTTP and HTTPS
  • Intercept errors
  • Great for routing

The code

var httpProxy = require('http-proxy');
httpProxy.createServer({
  hostnameOnly: true,
  router: {
    'domain1.com': '127.0.0.1:3001',
    'domain2.com': '127.0.0.1:3002'
  }
}).listen(80);

Thanks!

Fork me on Github

Fullscreen