Guide to easily deploy your express API as a serverless function for free using ZEIT.
Photo by Benjamin Voros on Unsplash (Just a beautiful picture, not related to the content)
Introduction
Serverless computing (or serverless for short), is an execution model where the cloud provider (AWS, Azure, or Google Cloud) is responsible for executing a piece of code by dynamically allocating the resources. And only charging for the number of resources used to run the code. The code is typically run inside stateless containers that can be triggered by a variety of events including HTTP requests, database events, queuing services, monitoring alerts, file uploads, scheduled events (Cron jobs), etc. The code that is sent to the cloud provider for execution is usually in the form of a function. Hence serverless is sometimes referred to as “Functions as a Service” or “FaaS”.
Initialization
Windows
If you are on windows, I highly recommend you download and install git bash. It comes bundled with git and can be downloaded from here: https://git-scm.com/downloads
Project setup
Go to your projects folder and open terminal or git bash if on windows. We will now create a new folder and change into it. Copy and paste the command below to do it.
mkdir express-serverless-example && cd express-serverless-example
Now we will install all the required dependencies for the project, make sure nodejs(You can get it here: https://nodejs.org/en/download/) is installed in your system. To make sure nodejs and npm are installed, you can run the below command to check the version of each.
node -v && npm -v
We will now initialize a new project, for that you run the command below. This will create a new package.json file with default options.
npm init -y
We will now install express.
npm i express
We will now install now (CLI tool for ZEIT) as a global dependency.
npm i -g now
Now open the newly created folder in your favourite code editor, if you are using vscode(You can get it here: https://code.visualstudio.com/), which I highly recommend, you can type the command below and it will open vscode.
code .
Express code
We will now create an ‘index.js’ file to create an express app(Filename MUST be ‘index.js’ for it to work with ZEIT) and paste the following contents into it.
const express = require("express");
const app = express();
const port = 3000;
// Body parser
app.use(express.urlencoded({ extended: false }));
// Home route
app.get("/", (req, res) => {
res.send("Welcome to a basic express App");
});
// Users route
app.get("/users", (req, res) => {
res.json([
{ name: "Akash", location: "India" },
{ name: "Abhishek", location: "India" },
]);
});
// Listen on port 5000
app.listen(port, () => {
console.log(`Server is running on port 3000
Visit http://localhost:3000`);
});
That’s it, we are done with our basic express app.
Hosting
If you haven’t already, create an account on https://zeit.co/, and type the below command in your terminal or git bash and follow instructions to log in.
now login
Before we host our app in ZEIT, we must create a configuration file for it. To do that create a new file called ‘now.json’ and paste in below contents.
{
"version": 2,
"builds": [{ "src": "index.js", "use": "@now/node-server" }],
"routes": [
{
"src": "/",
"dest": "/index.js",
"methods": ["GET"]
},
{
"src": "/users",
"dest": "/index.js",
"methods": ["GET"]
}
]
}
I’ll now explain each of the fields above, the version field will specify the ZEIT Now Platform version and builds field will specify which build to use and which file to use as source and routes field will specify all routes to use, this is the most important part so if you add a new route don't forget to include it here.
You can find more info about configuration here https://zeit.co/docs/configuration#introduction/configuration-reference
Now, we are all done, now you can run the command below to host your API on ZEIT.
now
The function should be successfully uploaded and you should get a link to access it.
Top comments (0)