Deploy a LoopBack API project using

Bram Borggreve
InstructorBram Borggreve

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson we will look at how to deploy our LoopBack API project using now.

We will install the now client, create an account and deploy our app.

Using now secrets features we will store our database connection url which we get by creating a free MongoDB Atlas database.

In order to simplify following deployments we will add a now key to our package.json where we use the name, alias and env parameters to configure our deployments. We will add some npm scripts so we can deploy our app in the desired way using npm run now.

[00:00] We install Now globally using npm. When the installation is finished, we run now --login to log in or create an account.

[00:08] Follow the instructions on the screen to confirm your account. Inside our loopback project, we run the command Now to start the deployment. This will upload our project to the Now servers and run npm install.

[00:21] We already get the deployment URL and when we open that in our browser, we can follow along with the progress. When the dependencies are installed, Now will try to start our server. We see that it fails because it cannot open db.json, the file that we have referred to in our in-memory data source. Let's fix this by connecting our API to the MongoDB instance on Atlas.

[00:43] We add our MongoDB URL as a secret to Now. Next, we start Now with the environment variable MondoDB URL set to @mongodb-URL. By using the @ sign in our environment variable, Now knows that it needs to use our secret as a value.

[01:00] Awesome. Our API is deployed and in the terminal, we can see that it's connected to MongoDB.

[01:05] The page in our browser is refreshed and we can visit the API explorer. We start by listing out our categories, which is an empty array. Using our Axis token, we see that we can add new categories. Now let's open up our package.json and create some scripts and configuration for Now. On the top level, we add a new property called Now.

[01:27] Inside this upcheck, we add a property called name, which is a string, which we set to egghead-loopback.

[01:34] Next, we add a property called alias, which is an array and in that array, we add a string called egghead-loopback. The last property we add is called env, which is an object and in this object, we add a key called mongodb_URL, which we set to @mongodb-URL.

[01:52] For good measures, we also set our node env to production. We then move to our script section, where we add a script called Now:deploy, which executes Now. We add a second script called NowAlias, which executes Now alias. The last script we add is just called Now and it invokes the other two scripts. We save our file.

[02:14] We hop over to our terminal and run npm run Now. We see that our project gets deployed and once that's done, we get an alias that we can use to access our API.

[02:24] One thing to note about using a free account of Now is that the deployments are public. This means it will show the source of the project you are running when visiting the underscore as our c path on your project. If you subscribe for the paid plan, your projects will be private by default.

Cristian Caroli
Cristian Caroli
~ 5 years ago

For some reason the string connection doesn't work properly when you add it as a secret (it's identical as the one you pass it on the video, with personal password, of course)


Connection fails: MongoError: connection 3 to closed It will be retried for the next request. /home/nowuser/src/node_modules/bluebird/js/release/async.js:61 fn = function () { throw arg; }; ^ MongoError: connection 3 to closed at Function.MongoError.create (/home/nowuser/src/node_modules/mongodb-core/lib/error.js:29:11) ´´´

Victor Hazbun
Victor Hazbun
~ 5 years ago

@cristian_caroli did you resolved the issue already?

~ 3 years ago doesn't operate like this lesson suggests anymore.