Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy Loopback ? Need simple explanation

I have developed a Loopback API and would like to deploy it to a test instance (Heroku or Digital Ocean, probably).

It is quite complicated to understand how to deploy it. There seems to be many solutions out there, from StrongLoop Process Manager to a plain Node.js server... The information is not very digest so could anyone help me out understand what possibilities I have to deploy it and what are the pro/cons of each one.

I am reading some documentation right now but feel a bit lost. Some input from people who have already deployed it would be great, I can't be the only one to feel confused at this point.

like image 724
kartsims Avatar asked Apr 18 '16 14:04

kartsims


People also ask

How does LoopBack framework work?

LoopBack applications access data through models, so controlling access to data means defining restrictions on models; that is, specifying who or what can read and write the data or execute methods on the models.

What is LoopBack programming?

LoopBack is a highly-extensible, open-source Node. js framework that enables you to: Create dynamic end-to-end REST APIs with little or no coding. Access data from major relational databases, MongoDB, SOAP and REST APIs. Incorporate model relationships and access controls for complex APIs.


3 Answers

First difference is if you are planning to host it on a server (Digital Ocean) or as a hosted node process (Heroku).

On Heroku you need to understand their way of doing it, but once done can save you a lot of worry on infrastructure management. This would not include StrongPM and Heroku is already it's own processmanager. If you have basic understanding of Git and don't use advanced stuff eg. with OS dependencies, this is the easiest in the long run. Don't have experience, but it would seem that Heroku works directly of a Git repository, so working with Git is a requirement for this model.

On a server (eg. digital ocean), you need to decide on an OS and set that up with dependencies, install node, DBs, ect. Once does you can manage your node process with a process manager, eg. StrongPM, though I've had mi battle with it and long ago decided to go with PM2 as process manager, which I found much easier to understand and handle, both initially and in the long run. But the basic is the same as on your local machine, you just need it to run "node server.js", a process manager just gives it some more bells and whistles (auto restart on error, monitoring and repeatable process start, being the main things).

like image 179
Mikael Jensen Avatar answered Oct 24 '22 03:10

Mikael Jensen


I have done this many times, and it can be fairly straightforward. The most complicated bit is setting up the database. For Heroku, you can't use filesystem storage (e.g. storing everything in a db.json file) because the filesystem is not persistent. So you need an external database, and luckily Heroku offers a few of these as addons, e.g. mLab for MongoDB which I highly recommend.

Once you've provisioned the database, make sure the correct details are configured for the datasource. Here is an example from a Heroku-hosted app using mLab (I've xxxx-ed out a few details):

"db": {
  "host": "ds043471-a0.mongolab.com",
  "port": 43471,
  "database": "heroku_appxxxxxxx",
  "username": "heroku_appxxxxxxx",
  "password": "xxxxxxxxxx",
  "name": "KaranMongo_live",
  "connector": "mongodb"
}

You can even test this locally now (though best practice is to use separate datasource json files for development/production).

Next, you need to make a few adjustments to make your application "Heroku-ready":

  1. Add the Strongloop buildpack, i.e. heroku buildpacks:set https://github.com/strongloop/strongloop-buildpacks.git
  2. Create a Procfile, which only needs one line: web: slc run

Then push to your heroku app (assuming you've set up the remote correctly):

git push heroku master

Magic. It builds and deploys.

like image 38
Anselan Avatar answered Oct 24 '22 02:10

Anselan


  1. Follow guide here to install NodeJS How To Set Up a Node.js Application for Production on Ubuntu 16.04 but instead of pm2 start hello.js enter your pm2 start server/server.js

  2. Edit your ngnix config file sudo nano /etc/nginx/sites-available/default

    location / { 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; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }

  3. Restart nginx sudo systemctl restart nginx

like image 2
Mohamed El-Saka Avatar answered Oct 24 '22 03:10

Mohamed El-Saka