Node.js Hello World

All the steps required to deploy a simple node app on a server.

Note: we will be creating an app for hello.ifactory.co and assume DNS entries for *.ifactory.co resolve to the server.

1. Pre-Steps

  • Have a Node Server prebuilt - see the other setup docs.

2. Node Hello World

Lets create a hello world Node app

$ mkdir /var/www/hello.ifactory.co/
$ touch /var/www/hello.ifactory.co/hello.js
$ nano /var/www/hello.ifactory.co/hello.js

Then paste into nano > hello.js the following code:

const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from NodeJS!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

3. Nginx Config

Enter the following

Paste in the following config:

4. Run with PM2

Related Commands

PM2 Setup and auto start on Reboot:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

PM2 Cheat sheet: https://devhints.io/pm2 https://pm2.keymetrics.io/docs/usage/quick-start/#restart-application-on-changes

5. Configure SSL ?

First check the requirements are in place:

  1. DNS A Records point to server e.g. hello.ifactory.co and www.hello.ifactory.co

  2. Check if Firewall allows SSL port 22. If not add.

  3. etc/nginx/sites-available/hello.ifactory.co.conf file exists and the server name is set to domains:

  4. Confirm Nginx Configuration is correct:

  5. Install Certbot

  6. Obtain SSL Cetificate

  7. Verifying Certbot Auto-Renewal

  8. Test the Certbot renewal process

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

6. Restart Nginx - OPTIONAL

After changes to Nginx configuraiton restart Nginx.

Was this helpful?