We install Now globally using npm. When the installation is finished, we run now --login to log in or create an account.
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.
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.
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.
Awesome. Our API is deployed and in the terminal, we can see that it's connected to MongoDB.
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.
Inside this upcheck, we add a property called name, which is a string, which we set to egghead-loopback.
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.
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.
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.
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.