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.
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.
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.
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).
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":
heroku buildpacks:set https://github.com/strongloop/strongloop-buildpacks.git
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.
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
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; }
Restart nginx
sudo systemctl restart nginx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With