Skip to content

Latest commit

 

History

History
163 lines (112 loc) · 3.3 KB

NOTES.md

File metadata and controls

163 lines (112 loc) · 3.3 KB

How to Deploy Express on Now.sh

In this notes I'm going to share how to setup express API endpoints to run on version 2 of Now.sh. You will get a free https endpoints and run in serverless! Isn't it cool?

You can find full source code at github.

Prerequisite

  • Now CLI (12.1.9)
  • Node (v10.10.0)
  • express (4.16.4)

Add Endpoints to express

For simplicity we are going to have 2 endpoints to show how to handle GET and POST requests.

/get - GET

This returns VERSION in json output.

Edit index.js

app.get("/get", (req, res, next) => {
  res.json({
    version: process.env.VERSION,
  });
});

/post - POST

Echo back JSON content being posted.

Edit index.js

app.post("/post", function (request, response) {
  response.send(request.body);
});

Storing Secret as Environment Variable

You may have noticed in the '/get' endpoint we used process.env.VERSION. This is a common practice not to hardcode secrets in code.

Set Environment Variables

export VERSION="1.0"

Deploy to now.sh

Setup Build for now

We need to setup build to use @now/node-server. (Using @now/node just won't work). Modify now.json

"builds": [{
    "src": "index.js",
    "use": "@now/node-server"
}]

Read more at doc.

Set Environment Variable as Secret in now.sh

now-linux secret add VERSION $VERSION

Read more at doc.

Allow CORS

Here we need to add custom response headers. Modify now.json

"routes": [{
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
        "Access-Control-Allow-Headers": "X-Requested-With, Content-Type, Accept"
    },
    "src": "/.*",
    "dest": "/index.js"
}]

Read more at doc.

Push to now.sh

now-linux

Sample output

❯ now-linux
> UPDATE AVAILABLE The latest version of Now CLI is 12.1.9
> Read more about how to update here: https://zeit.co/update-cli
> Changelog: https://github.com/zeit/now-cli/releases/tag/12.1.9
> Deploying ~/code/repo/github/express-now under XXXXXXX
> Synced 2 files (929B) [1s]
> https://express-now-3b57ke4d4.now.sh [v2] [in clipboard] [1s]
┌ index.js        Ready               [17s]
└── λ index.js (284.31KB) [sfo1]
> Success! Deployment ready [19s]

Tests

Spin up a localhost server.

npm start

Test /get

In terminal,

curl http://localhost:3000/get

Response

{"version":"1.0"}

Test /post

In terminal,

curl -H "Content-Type: application/json" \
-d '{"message":"hello"}' \
http://localhost:3000/post

Response

{"message":"hello"}

Note: You can replace localhost with the now.sh instance url.

Gotcha

Perhaps due to the nature of serverless sometime the endpoint returns 502 error. To tackle that please add retry mechanism to your service callers.