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:
$ 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:
$ pm2 init
- Modify the generated file to match Loopback requirements:
/ecosystem.config.js
</div>
module.exports = {
apps: [
{
name: 'MyAPI',
script: 'index.js',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development',
},
env_production: {
NODE_ENV: 'production',
},
},
],
};
- Add
pm2
as a dependency to your project using the following command
$ npm install pm2 --save
- Modify your
start
and add astop
script inpackage.json
to look likethis
{
"scripts": {
"start": "pm2 start ecosystem.config.js --env production"
"stop": "pm2 stop ecosystem.config.js --env production"
}
}
Deployment
- Register and start your application with
pm2
using the following command atthe app root directory:
$ npm start
This creates a dist folder which contains the transpiled code. Use index.js
at 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.conf
file. If you are not using the default config file, adjust accordingly.
location /fooapi {
proxy_pass http://localhost:3000;
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;
}
- 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 ontopm2
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
.