This manual is about setting up an automatic deploy workflow using nodejs, PM2, nginx and GitLab CI. It is tested on:
- Target server: Ubuntu 16.04 x64. This is suitable for Ubuntu 14.x.
- Windows 10 on my PC to work.
Steps to deploy a Node.js app to DigitalOcean using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt
If you use the referal link below, you get $10 free (1 or 2 months) https://m.do.co/c/5424d440c63a
I will be using the root user, but would suggest creating a new user
| /* | |
| * Handling Errors using async/await | |
| * Has to be used inside an async function | |
| */ | |
| try { | |
| const response = await axios.get('https://your.site/api/v1/bla/ble/bli'); | |
| // Success 🎉 | |
| console.log(response); | |
| } catch (error) { | |
| // Error 😨 |
| var express = require('express'); | |
| var app = express(); | |
| // response header for sever-sent events | |
| const SSE_RESPONSE_HEADER = { | |
| 'Connection': 'keep-alive', | |
| 'Content-Type': 'text/event-stream', | |
| 'Cache-Control': 'no-cache', | |
| 'X-Accel-Buffering': 'no' | |
| }; |
| console.log("Before the first file is read."); | |
| hypotheticalFileGetContents("sample.txt", function(fileContents){ | |
| // fileContents now contains the file contents, this function is only called when the file read in the background has finished | |
| console.log("After the first file has completed reading."); | |
| }); | |
| // You've now told it to start the first read, but it won't 'block' your script execution. It will do the read in the background, and immediately move on with the rest of your code. | |
| console.log("Before the second file is read."); | |
| hypotheticalFileGetContents("sample2.txt", function(fileContents){ |