Node.js Server Deployment

Dr. Greg M. Bernstein

April 11th, 2021

Node.js Server Deployment

Learning Objectives

  • Login and run programs on a Linux server
  • Be able to transfer files but not node_modules to the Linux server
  • Recreate your execution environment using NPM and package.json
  • Configure your Node.js sever to work with a reverse proxy
  • Keep your server running after you logout with PM2

Login to DrBsClasses.org

  • The server we will be using is DrBsClasses.org
  • You will receive an email with an account name and password from me.
  • Your account name will be something like studentN where N is some number (I’m student1 and student70)
  • Use Putty or SSH command line (ssh student1@drbsclasses.org)

Running Programs

  • NPM (npm), Node.js (node), and common Linux commands are available
  • PM2 a process manager for Node.js is also installed for you to use

Transferring Files

  • The Linux server at “DrBsClasses.org” supports SFTP (SSH FTP)
  • This is the same type of “FTP” used with the department servers
  • DO NOT transfer the node_modules directory. Recreate it with npm install and package.json
  • Create a directory to hold your server version for each homework assignment

Configuring your Node.js Server

  • You will be given an account name studentN where N is and integer
  • You will have your Express.js/Node.js server listen on IPv4 address 127.0.0.1 and port 3000 + N
  • For example I’m student1 so my server listens on port 3001

My Configured Server

const express = require('express');
const app = express();
const port = 3001; // Should be 3000 + N, where you are studentN
// I'm student1
let count = 0; // Visit count
let startDate = new Date(); // Server start Date time

app.get('/', function (req, res) {
    count++;
    res.send(`Hi from Dr. B., Visited: ${count} times.`);
});

app.get('/uptime', function(req, res){
    let curDate = new Date();
    res.send(`Current Date/Time: ${curDate.toLocaleString()}, Server Up Since: ${startDate.toLocaleString()}`);
})

host = '127.0.0.1';

app.listen(port, host, function () {
console.log(`deployTest.js app listening on IPv4: ${host}:${port}`);
});

Running Your Server

  • Recreate node_modules (only have to do this once)
  • Use the command node serverName (just like you do locally)
  • Use ctl+Cto stop the server

Reaching Your Server From a Browser

  • Domain Name and Path Prefix: https://www.drbsclasses.org/studentN/node where N is an integer from your assigned account name.
  • For example https://www.drbsclasses.org/student1/node is my student1 server
  • Paths extend from this base such as: https://www.drbsclasses.org/student1/node/uptime

Use Relative URLs!!!

  • Note that / is absolute but ./ is relative!
  • Check entries in all templates and forms to make sure they are relative
  • Test all your links

Debugging Your Server

  • Start and stop the server as needed
  • Update files as needed
  • Test and fix

Managing Your Server

  • To manage your server so that it runs properly after you logout use PM2
  • Start the server: pm2 start serverName.js
  • Stop the server: pm2 stop serverName.js
  • See the PM2 documentation for more info.
// reveal.js plugins