pauls page

for music, code and the internet of things.

Node.js Cheat Sheet

Installing on Ubuntu 18.04

Install Node.js from the repositories

$ sudo apt install nodejs

Install npm, the Node.js package manager

$ sudo apt install npm

Check which version of Node.js you have installed

$ nodejs -v

Create your project

mkdir project
cd project

Initialize your project.

npm init

Link the Express package.

npm install express --save

Express Hello World

$ vi index.js

const express = require('express')
const app = express()
const port = 80
app.get('/', (req, res) => doHomepage(res))
app.listen(port, () => console.log('App: Listening on port '+port))

function doHomepage(res) {
  res.send('Hello World!')
}

Run it with PM2

Use npm to install the latest version of pm2 on your server

$ sudo npm install pm2@latest -g

$ pm2 start index.js

To restart/kill all processes

$ pm2 restart all