This is a basic guide for deploying a LoopBack 4 (LB4) app usingpm2 behind nginx reverse proxy.

pm2 is a Production Runtime and Process Manager for Node.js applications witha built-in Load Balancer. It allows you to keep applications alive forever, toreload them without downtime and facilitate common Devops tasks.

NGINX is open source software for web serving, reverse proxying, caching, loadbalancing, media streaming, and more. It started out as a web server designedfor maximum performance and stability. In addition to its HTTP servercapabilities, NGINX can also function as a proxy server for email (IMAP, POP3,and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

NOTE: Production deployment is a detailed topic and explained with clarityin their respective documentations. Please refer pm2 docsandnginx docsfor detailed setup instructions. This guide assumes you have nginx setupalready.

First steps

Loopback app

Before we start with deployment, let’s get our app ready. If you have Loopbackcli installed, run the following command to create a new app:

  1. $ lb4 app

Refer togetting startedsection of documentation for detailed instructions.

pm2 setup

You might find yourself in a situation in which you do not have access to theCLI to start your Node.js applications. In such a situation, pm2 must beadded as a dependency and must be called with the start script.

  • Generate an ecosystem.config.js template with:
  1. $ pm2 init
  • Modify the generated file to match Loopback requirements:

/ecosystem.config.js

</div>

  1. module.exports = {
  2. apps: [
  3. {
  4. name: 'MyAPI',
  5. script: 'index.js',
  6. instances: 1,
  7. autorestart: true,
  8. watch: false,
  9. max_memory_restart: '1G',
  10. env: {
  11. NODE_ENV: 'development',
  12. },
  13. env_production: {
  14. NODE_ENV: 'production',
  15. },
  16. },
  17. ],
  18. };
  • Add pm2 as a dependency to your project using the following command
  1. $ npm install pm2 --save
  • Modify your start and add a stop script in package.json to look likethis
  1. {
  2. "scripts": {
  3. "start": "pm2 start ecosystem.config.js --env production"
  4. "stop": "pm2 stop ecosystem.config.js --env production"
  5. }
  6. }

Deployment

  • Register and start your application with pm2 using the following command atthe app root directory:
  1. $ npm start

This creates a dist folder which contains the transpiled code. Use index.jsat your app’s root level for starting the server using pm2.

Now you can visit http://127.0.0.1:3000/ to checkyour newly deployed API.

  • Configure nginx reverse proxy by adding following rule to your nginx.conffile. If you are not using the default config file, adjust accordingly.
  1. location /fooapi {
  2. proxy_pass http://localhost:3000;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection 'upgrade';
  6. proxy_set_header Host $host;
  7. proxy_cache_bypass $http_upgrade;
  8. }
  • All set! Now you can hit your localhost at http://localhost:3000/fooapi(assuming nginx is listening to port 80) and your requests will be passed onto pm2 process running your Loopback application.

NOTE: This is one of the many ways to expose your APIs. If you notice, thereare three main components to this recipe. A node application, a process managerand a reverse proxy server. Since Loopback is the node application in ourcontext, this will be a constant thing. You can choose any process manager fornode and any server instead of pm2 and nginx.