My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Serverless with Vercel

saurshaz's photo
saurshaz
·Aug 9, 2020·

4 min read

  • use vercel.json to declare runtimes and routes you want in your application. Generally this will be one runtime, but vercel supports ruby, golang, python and nodejs
{
    "builds": [
        {
            "src": "**/*.html",
            "use": "@now/static"
        },
        {
            "src": "**/*.py",
            "use": "@now/python"
        },
        {
            "src": "**/*.js",
            "use": "@now/node"
        },
        {
            "src": "**/*.go",
            "use": "@now/go"
        }
    ],
    "routes": [
        {
            "src": "/",
            "dest": "public/index.html"
        },
        {
            "src": "/py",
            "dest": "api/hello.py"
        },
        {
            "src": "/flask",
            "dest": "api/flask.py"
        },
        {
            "src": "/ping",
            "dest": "api/ping.py"
        },
        {
            "src": "/js",
            "dest": "api/hello.js"
        },
        {
            "src": "/go",
            "dest": "api/app_go.go"
        }
    ]
}
  • create a directory called api. This ius where your route handlers will live. would be simple or complex logic in js, go, py or rb files. Example logics are give below

hello.py

from http.server import BaseHTTPRequestHandler

class handler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/plain')
        self.end_headers()
        message = 'Hello from Python from a Serverless Function!'
        self.wfile.write(message.encode())
        return

hello.js

module.exports = (req, res) => {
    res.json({
        body: req.body,
        query: req.query,
        cookies: req.cookies
    })
}
  • in case you are using modules/packages, you can declare the same in requirements.txt file (for python) and package.json (for nodejs)
  • For detailed instructions you can refer to links in referfences section below

Running in development environment

  • run vercel dev
  • nodejs function is in action at http://localhost:3000/js
  • python function is in action at http://localhost:3000/py

Similarly, more runtimes will work. As mentioned if this seems tolo low-level, you can also have modules like bottle , flask to do server stuff in lesser lines in python (install modules locally, setup venv and doi pip install flask for that, and modify code accordingly. an example is shown below)

flask.py

from flask import Flask, Response
app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return Response("<h1>Flask</h1><p>You visited: /%s</p>" % (path), mimetype="text/html")

More on advanced usage here.

Also note, you can code in rust, deno, php etc. with help of community built runtimes.

Running in stage or prod environment

  • As with all things vercel, run vercel or vercel --prod to get the endpoints on the public internet in your vercel account. You can then attach your domain also there.

References

  • Awesome vercel documentation here
  • Brilliant PoC by rodrigodrds here