How to run Express.js API on ZEIT Now?

06/01/2019 — 2 Min Read — In Node.js

Let's run Node server using ZEIT's Now! There are a couple of options to host any Node.js API. You can go with own VPS and use PM2 to manage processes, use Docker or host it at Heroku and many more. For a long time I used CapRover (previously CaptainDuckDuck), but it requires installing VPS server and taking care of it. Under the hood, it uses both Docker and PM2 mentioned earlier.

What you can try is hosting your Node API using Now (v2). As we read on their home page:

Now makes serverless application deployment easy. Don’t spend time configuring the cloud. Just push your code.

That's the whole essence of Now, it describes perfectly how easy usage of it is. Because we love coding, not struggling with server configuration.

First steps

Register an account. I recommend to signup with GitHub account.

Install Now for GitHub (optional). It will allow you to deploy your apps/APIs/anything when you commit new changes.

Install Now CLI. Type yarn global add now in your command line. Unfortunately, that method didn't work for me (Windows 10, Bash command line).

And you're ready to go!

Init project

Create a new directory for your project. Then in terminal type yarn init -y and install Express.js yarn add express.

Add file index.js. That's where our code will live. Inside of it paste:

const express = require("express")
const app = express()
app.get("/", (req, res) => {
res.json({
message: "Hello World"
})
})
module.exports = app

Let's now deploy with Now

What is the most important, each project we would like to deploy on Now, must contain now.json config file which describes what our app is and how Now should take care of it. In our case code will be turned into serverless lambdas. Let's put in below content:

{
"version": 2,
"name": "joy-with-now",
"builds": [{ "src": "**/*.js", "use": "@now/node" }]
}

You can find a full list of available options in documentation. Let's describe each of used above:

  • version - Now Platform version. Can be 1 or 2 (for new accounts I think only 2)
  • name is just the name of your app. This is how it will be named in ZEIT dashboard and how will start URL to that deployment.
  • builds specifies what to do with .js files. They will go to Node.js builder which will transform these.

Add the file .nowignore. It's similar to .gitignore and says what files/directories should be skipped when deploying. Inside add only node_modules.

We're ready to deploy! Just type now in the command line and wait until the whole process finishes. Woohoo, your API is live. Visit URL which appeared in the console (that's mine https://joy-with-now-rbhuj57cn.now.sh/) and see if you've got that response:

{
"message": "Hello World"
}

You can drink some green tea now and be happy! Each time you'll want to deploy, just use the command now. So simple.

Automatic deploy with GitHub

If you authorize Now to access your GitHub account, it will automatically deploy changes pushed to repositories containing file now.json. I will write more about it another article.

ZEIT examples

ZEIT offer very good documentation on how to use their product. All the above code is almost identical to their example repo. Check other examples on how to deploy other kinds of apps here.

Summary

We've deployed our API in a couple of minutes. That was quick and easy. I'm going to write a few more articles about ZEIT as I had some issues with moving an existing project to it. I will describe how I solved each of these. Maybe people trying to use it for the first time will save some time.