Tag Archives: node.js

npm package.json

  • ~version “Approximately equivalent to version” See npm semver – Tilde Ranges & semver (7)
  • ^version “Compatible with version” See npm semver – Caret Ranges & semver (7)
  • version Must match version exactly
  • >version Must be greater than version
  • >=version etc
  • <version
  • <=version
  • 1.2.x 1.2.0, 1.2.1, etc., but not 1.3.0
  • http://sometarballurl (this may be the URL of a tarball which will be downloaded and installed locally
  • * Matches any version
  • latest Obtains latest release

References
http://stackoverflow.com/questions/22343224/difference-between-tilde-and-caret-in-package-json

PM2 CheatSheet

 

# Fork mode
$ pm2 start app.js --name my-api # Name process

# Cluster mode
$ pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
$ pm2 start app.js -i max      # Same as above, but deprecated yet.

# Listing

$ pm2 list               # Display all processes status
$ pm2 jlist              # Print process list in raw JSON
$ pm2 prettylist         # Print process list in beautified JSON

$ pm2 describe 0         # Display all informations about a specific process

$ pm2 monit              # Monitor all processes

# Logs

$ pm2 logs [--raw]       # Display all processes logs in streaming
$ pm2 flush              # Empty all log file
$ pm2 reloadLogs         # Reload all logs

# Actions

$ pm2 stop all           # Stop all processes
$ pm2 restart all        # Restart all processes

$ pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)
$ pm2 gracefulReload all # Send exit message then reload (for networked apps)

$ pm2 stop 0             # Stop specific process id
$ pm2 restart 0          # Restart specific process id

$ pm2 delete 0           # Will remove process from pm2 list
$ pm2 delete all         # Will remove all processes from pm2 list

# Misc

$ pm2 reset <process>    # Reset meta data (restarted time...)
$ pm2 updatePM2          # Update in memory pm2
$ pm2 ping               # Ensure pm2 daemon has been launched
$ pm2 sendSignal SIGUSR2 my-app # Send system signal to script
$ pm2 start app.js --no-daemon
$ pm2 start app.js --no-vizion
$ pm2 start app.js --no-autorestart
$ pm2 start app.js           # Start app.js

$ pm2 start app.js -- -a 23  # Pass arguments '-a 23' argument to app.js script

$ pm2 start app.js --name serverone # Start a process and name it as serverone
                                    # you can now stop the process by doing
                                    # pm2 stop serverone

$ pm2 start app.js --node-args="--debug=7001" # --node-args to pass options to node V8

$ pm2 start app.js -i 0             # Start maximum processes depending on available CPUs (cluster mode)

$ pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm Z"    # Log will be prefixed with custom time format

$ pm2 start app.json                # Start processes with options declared in app.json
                                    # Go to chapter Multi process JSON declaration for more

$ pm2 start app.js -e err.log -o out.log  # Start and specify error and out log
$ pm2 start echo.pl --interpreter=perl

$ pm2 start echo.coffee
$ pm2 start echo.php
$ pm2 start echo.py
$ pm2 start echo.sh
$ pm2 start echo.rb
{
  ".sh": "bash",
  ".py": "python",
  ".rb": "ruby",
  ".coffee" : "coffee",
  ".php": "php",
  ".pl" : "perl",
  ".js" : "node"
}

References
http://pm2.keymetrics.io/docs/usage/quick-start/#cheatsheet

Sessions in Express.js

var session = require('express-session');
const MongoStore = require('connect-mongo')(session);
app.use(session({
    key: 'ERP.Session',
    secret: '310E56DD8E7C',
    resave:true,
    saveUninitialized:true,
    store: new MongoStore({
        url: 'mongodb://localhost/ERP'
    })
}));

Setting Session Variables

req.session.name = 'Napoleon';
req.session['primary skill'] = 'Dancing';

Reading Session Variables

var name = req.session.name;
var primary_skill = req.session['primary skill'];

Updating Session Variables

req.session.skills.push('Baking');
req.session.name = 'Pedro';

Deleting Session Variables

delete req.session.name
delete req.session['primary skill'];

Deleting a Session

req.session.destroy();
req.session.destroy(function() {
  res.send('Session deleted');
});

References
http://expressjs-book.com/index.html%3Fp=128.html
https://github.com/expressjs/session
https://github.com/jdesboeufs/connect-mongo