How to Deploy Express on Now.sh
In this post 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.
Top comments (8)
How to get
now.sh
instance url ?When you deploy your code with
now-linux
(never heard ofnow-linux
but justnow
yes, I have to check), the terminal should output the URL... it even write it on the clipboard (the memory instance when you copy/paste) this URL.I got the Linux cli here. You will use
now
if you installed vianpm
What i mean is to get instance url at runtime in your app
As @Gli said once you push code to now, intance url will be printed on console. Notice the line
https://express-now-3b57ke4d4.now.sh
is printed.Sample output
❯ now-linux
Visit zeit.co/now here to see more details
No no, what i mean is, inside my
index.js
code, i want to get the instance url likeprocess.env.NOW_URL
, like in Now V1.Zeit makes amazing APIs. Good introduction!
I have created Express application using express-generator.So,what are the steps I need to follow ?