FeathersJS Deployment
Adam C. |

In my opinion, the FeathersJS is the easiest way to set up a Restful API. If you want to learn more about FeathersJS, please check the official doc or the article I wrote.

Photo by David Clode on Unsplash

Add Production Start Script 

FeathersJS runs on Node Server, so there is no such thing called  “npm run build".  On the local, I usually run “ npm run dev”, which uses config/default.json, and by default starts on port 3030.  You may think we could run “npm start” on the production, but it's quite right. Because “npm start” does the same thing as “npm run dev” except the latter is running in the hot load mode. So before we push the code the production,  we should make some changes to the package.json. What I did is adding another script named “prod”, like below:

"scripts": {
    "test": "npm run lint && npm run mocha",
    "lint": "eslint src/. test/. --config .eslintrc.json --fix",
    "dev": "nodemon src/",
    "start": "node src/",
    "prod": "PORT=5050 NODE_ENV=production node src/",
    "mocha": "mocha test/ --recursive --exit"
  },

And then, when we run “npm run prod”, the application will run on PORT 5050, and use config/production.conf.

Apache Configuration

When we start a node application, it's running on the localhost, which could not be accessed from external. One way to fix this issue is to use Apache webserver as a proxy -  the request to Apache is sent to the node server.  Below is an example configuration file, which is saved under “/etc/apache2/sites-available”:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    RequestHeader set X-Forwarded-Proto "https"
    ProxyPreserveHost On
    ServerName api.feathersjs.example
    Alias /static /var/www/FEATHERSJS_APP/public
    <Directory /var/www/FEATHERSJS_APP/public>
        Options FollowSymLinks
    </Directory>
    Header always unset X-Frame-Options
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket
    RewriteRule /(.*)           ws://localhost:5050/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket
    RewriteCond %{REQUEST_URI} !/static/
    RewriteRule /(.*)           http://localhost:5050/$1 [P,L]


    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/api.feathersjs.example/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/api.feathersjs.example/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>
</IfModule>

<VirtualHost *:80>
ServerName api.feathersjs.example
ServerAlias www.api.feathersjs.example
Redirect "/" "https://api.feathersjs.example/"
</VirtualHost>

Note that we use Let's Encrypt - a free SSL service. You may check this article to get started.

Start/Stop Node Application with PM2

Without PM2, we can start Node Application with, “npm run prod”, and it will be running on the console, if you accidentally close the console, the process will be ended. To make the process running in the background, and still be able to control it whenever we want, PM2 comes to the place. 

First of all, install the PM2 by “npm install -g pm2”,  then run the following command to start the Node application in the background:

pm2 start npm --name "feathers-dna" -- run prod

To check the status:

pm2 status

To stop a service:

pm2 stop feathers-dna //note: here feathers-dna is App name defined when we ran "pm2 start"

To delete a service:

pm2 delete feathers-dna

To check the log:

pm2 log

To learn more about PM2, please check out their office doc here.

I know I skipped a lot of details here. If you are stuck, please leave the message below. I will try my best to help you. 

Happy coding!