Creating Serverless Functions with Vercel
Tags: serverless, javascript, programming, node
Published: 2021-10-22 12:35:31

## Introduction
Looking for a serverless hosting service is a big pain when you just want to taste or even host a small project on it. Today, I found a new resource <u>which lets you create serverless functions for absolutely free!</u> **Vercel**. So, lets <u>build & deploy our first Serverless function</u> on it!

## Setup
The platform requires us to install its **CLI** in order to develop & deploy our functions. Lets do `npm i -g vercel` <u>to install the CLI globally</u>. After creating a new directory, <u>we'll initialize it as a new node project</u>.
![new node project init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pip00f95cz1mztnc1ac.png)

## Development Workflow
Vercel cli expects us to put all of our `.js` files in a folder called `/api`.
The `.js` <u>files will contain 1 default exported function which represents a __REST API endpoint__</u> taking 2 parameters `request` & `response` <u>representing the express's `Request` & `Response` objects respectively</u> (just like in normal `express.js` GET/POST endpoints).
The url at which the endpoints will be hosted will be `/api/{name-of-file}`

## Creating Our First Serverless Function
So I have created a basic `node.js` project with following files:
![basic serverless nodejs app file structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vulqgwi2f1sqgn5o4237.png)

![index.js file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9x5fy7tnl4neqo9j2r46.png)

![hello.js file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vnkbznhblkt8ylfx622.png)

"**But how I will develop them without even testing?**"

## CLI To The Rescue
The <u>vercel cli</u> which we just just installed globally __provides a functionality to run the functions on our own local machines__.
<u>You will be required by the CLI to login/signup into a vercel account, join me from here after completing that ;)</u>
Now, to run them, we must do `vercel dev`. <u>For the first time running the cli, it requires us to do some quick configs & settings</u>, after completing that you should see something like this:
![vercel cli setup and development environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en74ygoyvjx0pnodbggi.png)

To check how the functions are working locally, we have to go to `http://localhost:3000/api` & to access the endpoint in `hello.js`, we will go to `http://localhost:3000/api/hello`

![index.js serverless function endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnxh5vmwksgf5e1cw5np.png)

![hello.js serverless function endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf2n8ynvdy66ruafflk9.png)

Everything looks awesome! lets deploy it on vercel!

## Deploying Functions on Vercel
Vercel's CLI makes <u>deploying functions as easy as running a command on the terminal<u/>, we'll now run `vercel` on our terminal in the root of the directory.
![deplying serverless function with 1 command on vercel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2p2z6c5vyxgojr9lji1.png)

**HERE YOU GO!**
![deployed](https://media.giphy.com/media/Is1O1TWV0LEJi/giphy.gif)

_You can check limits for a free plan set by the platform [here](https://vercel.com/docs/concepts/limits/overview#websockets)_.