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.jsThen 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:
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:
DNS A Records point to server e.g. hello.ifactory.co and www.hello.ifactory.co
Check if Firewall allows SSL port 22. If not add.
etc/nginx/sites-available/hello.ifactory.co.conf file exists and the server name is set to domains:
Confirm Nginx Configuration is correct:
Install Certbot
Obtain SSL Cetificate
Verifying Certbot Auto-Renewal
Test the Certbot renewal process
6. Restart Nginx - OPTIONAL
After changes to Nginx configuraiton restart Nginx.
Was this helpful?
