Uploading the ReactJS app on Netlify is quite easy but API is not as simple as a static web app, it usually has some functional component that requires to be executed to generate a response based on the request endpoint. In this tutorial, you will learn how to deploy ExpressJS API on the Netlify platform as quickly as 10 mins.
We have created a Git repository with all code mentioned in this article. The steps explained below are linked to the files from that repo only. So if you prefer to dive into code directly then here is the link for that Git repo.
Steps Deploy ExpressJS API On Netlify
Step 1: If you haven’t created a project yet then first create a project directory and navigate to that directory using
Step 2: Initialize
npm and which will create a default
npm init -y
Step 3: Run the below command to install npm packages:
npm install express serverless-http netlify-lambda
Step 4: Create
.gitignore file and include this content
Step 5: Create
netlify.toml file which will have Netlify deploy configuration
Step 6: Create a directory with a name
dist and a
index.html file in that new directory. You can add some static content if you would like.
Note: Netlify will show the content of this index.html file if the user navigates to the root API domain.
Step 7: Create a new directory
api.js file in that new directory which will contain ExpressJS endpoint code
Note: We have used
app.use() because we have used the same value in
Step 8: Add these scripts in your
“start”: “netlify-lambda serve src”,
“build”: “netlify-lambda build src”
Step 9: To test this code run the below command and navigate to
localhost:9000/.netlify/functions/api: will return what you have configured
localhost:9000/.netlify/functions/api/another: will return what you have configured
Step 10: Now go ahead and deploy this API using the below command if you prefer
Netlify CLI or commit everything to a Git repo and deploy it in Netlify UI.
This way you can easily deploy ExpressJS API on the Netlify platform. If you need any help then feel free to ask your questions in the comment section below.