How to build and deploy a Node API on Vercel Serverless functions

Get your REST API deployed in less than 10 minutes!

Vercel Serverless Functions

Setting up

yarn global add vercelvercel login
mkdir vercel-apicd vercel-apiyarn init -ymkdir api
yarn add vercel -D
{"name": "vercel-api","version": "1.0.0","main": "index.js","license": "MIT","scripts": {"start": "vercel dev","deploy": "vercel deploy --prod"},"devDependencies": {"vercel": "^19.1.1"}}
import { NowRequest, NowResponse } from "@vercel/node";export default (req: NowRequest, res: NowResponse) => {   return res.json({ message: "Hello World" });};
mkdir api/userstouch "api/users/[id].ts"
import { NowRequest, NowResponse } from "@vercel/node";
const users = ["Bob Smith", "Guillaume Bibeau"];
export default (req: NowRequest, res: NowResponse) => {const { id } = req.query;res.status(200).json({ user: users[Number(id)] });};

Deploying

yarn deploy

Conclusion

💻 💻 Software Engineer at @filevine . 📖 📖 Instructor for @udacity . ✍️ ✍️ Writer at http://frontend-devops.com