Open In App

How to Deploy ExpressJS on Netlify ?

This guide explores the process of deploying an Express.js application on Netlify, a popular platform known for hosting web projects with ease. From understanding the basics of Express.js to configuring serverless functions on Netlify, this comprehensive guide offers step-by-step instructions in simple English, making the deployment process accessible to developers of all levels. Whether you're a seasoned developer or just starting, this guide equips you with the knowledge and tools to deploy your Express.js application confidently and efficiently. Let's dive in and bring your Express.js project to life on the web!

Prerequisites:

Approach:

Steps to create the application:

Step 1: Create a Simple Express Application.

Open your terminal and type:

mkdir my-express-app
cd my-express-app

npm init -y
npm i express netlify-cli netlify-lambda serverless-http

Create the file named netlify.toml

[build]
functions = "functions"

[dev]
publish = "dist"

Create the folder named dest inside make the empty index.html for the static files

Create a folder named functions inside make file named server.js in your project directory

const express = require('express');
const serverless = require('serverless-http');
const app = express();
const router = express.Router();

let records = [];


router.get('/', (req, res) => {
  res.send('App is running..');
});

router.post('/add', (req, res) => {
  res.send('New record added.');
});

router.delete('/', (req, res) => {
  res.send('Deleted existing record');
});

router.put('/', (req, res) => {
  res.send('Updating existing record');
});


router.get('/demo', (req, res) => {
  res.json([
    {
      id: '001',
      name: 'Aayush',
    },
    {
      id: '002',
      name: 'rohit',
    },
    {
      id: '003',
      name: 'Mohit',
    },
  ]);
});

app.use('/.netlify/functions/api', router);
module.exports.handler = serverless(app);

Project folder structure:

Screenshot-2024-04-12-002004

folder structure

Step 2: Set Up Git Repository: To make our project accessible and manageable, we'll upload it to GitHub. First, we navigate to GitHub and sign in to our account. Then, we create a new repository, naming it something like "simple-express-app". Selecting the option to initialize the repository with a README file is optional for our purposes. Next, we follow the provided instructions to add a remote repository to our local project and push our code to GitHub. These commands, executed in the terminal, establish the connection between our local project and the GitHub repository, making our code accessible from the cloud and enabling collaboration with others. With our project now hosted on GitHub, we're ready to proceed with deploying it on Netlify.

Step 3: Deploying Express.js Application on Netlify

Recording-2024-04-12-090545

output

Step 4: Testing the Deployment

Recording-2024-04-12-092032

output

Article Tags :