This is a barebone nodejs v8.9.1 project aimed at absolute beginners at Node.js.
I've always felt that templates generated by Nodejs were very confusing(sometimes, the port settings & listening parts are hidden in a /bin, and so on...).
The articles that explained nodejs pretty well were often very outdated.
In order to change all that, I've created a barebone repository with minimal but accurate routing, a sample of using config files (WARNING : NEVER INCLUDE YOUR CONFIG FILES IN AN ACTUAL GIT COMMIT! THEY SHOULD BE OUTSIDE OF THE GIT PROJECT DIRECTORY), an exposed code version of http server init/listening, and much more to come!
# installing nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
# clone this repository & move into the directory
git clone https://github.com/gyuhyeon/HelloNode.git
cd HelloNode
# install dependencies according to package.json
sudo npm install
# run the app
node app.js
# (optional) use PM2 for persistent process management
sudo npm install -g pm2
sudo pm2 start app.js # start process
sudo pm2 monit # monitor process
sudo pm2 list # list processes
sudo pm2 save # save current process list
sudo pm2 startup # reload process list even after rebooting
Node.js can act as a standalone web server, but it's more stable/robust if nginx is used as a proxy server and node.js receives requests through nginx.
The structure is as follows:
[Node.js app] <- HTTP(but port can be anything) -> [Nginx] <- HTTP(80)/HTTPS(443) -> [CLIENT]
- Install nginx
$ sudo apt-get update && sudo apt-get upgrade -y
$ sudo apt-get install nginx -y
- nginx checkup
$ sudo systemctl status nginx # To check the status of nginx
$ sudo systemctl start nginx # To start nginx
$ sudo systemctl enable nginx
- Configure nginx to act as a proxy
$ sudo rm /etc/nginx/sites-available/default # we don't need preset configs
$ sudo vi /etc/nginx/sites-available/default # make new one
server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://localhost:9000; # change 9000 to whatever port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
- test nginx config
$ sudo nginx -t
$ sudo /etc/init.d/nginx reload # reload if config was OK
- make node.js server listen to private IP at port specified in config above
const port = 9000;
app.listen(port);
- Install certification with certbot(let's encrypt)
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx
$ sudo certbot --nginx # not recommended : certonly option will only generate keys and not fiddle with options.
- set automated renewals
$ sudo certbot renew --dry-run # check if it works
$ sudo crontab -e
sudo certbot renew --quiet
- check nginx config
The file below will be automatically generated well if setting up HTTPS with Nginx was done AFTER configuring it well with HTTP and it was working(as guide above).
If not, the proxy_pass and etc may need to be tweaked.
Only thing that will not be created normally is the "proxy_set_header" part.
# /etc/nginx/sites-available/default
server {
listen 80;
server_name your_domain.com;
location / {
proxy_set_header X-Forwarded-Proto https; # important. this lets the node server know that it's actually secure.
proxy_pass localhost:9000; # change 9000 to whatever port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/liveyour_domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
} # managed by Certbot
}