Skip to main content

How to Deploy an Express.js App

In this tutorial, you will learn how to deploy an existing Express.js app using Genezio, a serverless deployment platform that simplifies app management and reduces costs. We'll cover the benefits, answer common questions, and provide detailed deployment steps.

Do I Need to Modify My Express App Code? Just make sure that your code is written in a stateless manner. Read more

Why Use Genezio?

Benefits of Using Genezio

  1. Faster Deployment: Genezio optimizes the deployment process, significantly reducing the time it takes to get your app live.
  2. Lower Costs: Pay only for the resources you use, thanks to Genezio’s efficient infrastructure and scalable solutions.
  3. Reliable and Scalable: No longer worry about PM2 and Docker. You write the code, and Genezio seamlessly scales it across multiple cores.
  4. Enhanced Security: Built-in security features and automatic updates protect your app against the latest threats without additional effort.
  5. Reduced Management Overhead: Focus on developing your app instead of managing servers and routine maintenance tasks.
  6. Easy Migration: Obtain all these advantages with minimal code changes.

How Does Genezio Handle High Traffic?

Genezio, being a Function as a Service (FaaS) platform, automatically scales your application based on traffic demands. It works by executing functions in response to events and can handle a large number of requests concurrently. This ensures your app remains responsive and performs well during traffic spikes.

Do I Need to Modify My Express App Code?

Migrating your Express app to Genezio typically requires minimal changes. Our migration guide provides detailed instructions for necessary compatibility adjustments.

Prerequisites

1. Install genezio

Use your prefered package manager to install genezio:

npm install genezio -g

2. Ensure you have an Express.js App

If you don't have an Express.js app, you can create one using the following steps:

Create a Hello World Express.js App

1. Initialize a New Node.js Project

Run the following command to initialize a new Node.js project in an empty directory:

npm init -y

2. Install Express.js

Next, install the Express.js package:

npm install express

3. Create an Express.js App

Create a new file named app.mjs and add the following code:

app.mjs
  import express from "express";

const app = express();

app.get("/", (req, res) => {
res.send("Hello World from Express!");
});

app.get("/users", (req, res) => {
res.json([
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
]);
});

app.listen(8080, () => {
console.log(
"Server is running on port 8080. Check the app on http://localhost:8080"
);
});

4. Test the Express.js App

Run the following command to start the Express.js app:

node app.mjs

Open a web browser and navigate to http://localhost:8080 to see the app running.

Deployment Guide

1. Install serverless-http

First, you need to install the serverless-http package.

Run the following command in the root directory of your Express.js app:

npm install serverless-http

This package allows you to wrap your Express.js application and deploy it on serverless environments.

2. Export the App as a Handler Function

You need to export your Express app as a handler function that can be used by Genezio.

Add the following code to your main application file (app.mjs or app.js):

app.mjs
  import express from "express";
import serverless from "serverless-http";

const app = express();

app.get("/", (req, res) => {
res.send("Hello World from Express!");
});

app.get("/users", (req, res) => {
res.json([
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
]);
});

// You don't need to listen to the port when using serverless functions in production
if (process.env.NODE_ENV === "dev") {
app.listen(8080, () => {
console.log(
"Server is running on port 8080. Check the app on http://localhost:8080"
);
});
}

export const handler = serverless(app);
info

You need to add "type": "module" in your package.json file.

This code wraps your Express app with the serverless-http package and exports it as a handler for Genezio.

3. Create the Genezio Configuration File

Now, create a genezio.yaml file in the root directory of your project.

This file will contain the configuration needed to deploy your backend using Genezio. Here is an example configuration.

info
  1. You might need to replace the entry field with the name of your main application file.
  2. You might need to replace the path field with the path relative at genezio.yaml file.
  3. This example configuration works if genezio.yaml is in the same directory as your main application file.
genezio.yaml
# The name of the project.
name: express-app
# The region where the project is deployed. Available regions: us-east-1, eu-central-1
region: us-east-1
# The version of the Genezio YAML configuration to parse.
yamlVersion: 2
backend:
# The root directory of the backend.
path: ./
# Information about the backend's programming language.
language:
# The name of the programming language.
name: js
# The package manager used by the backend.
packageManager: npm
# Information about the backend's functions.
functions:
# The name (label) of the function.
- name: hello-world-express-app-function
# The path to the function's code.
path: ./
# The name of the function handler
handler: handler
# The entry point for the function.
entry: app.mjs
# The compatibility of the function handler.
type: aws

This configuration file specifies the project name, deployment region, and details about the backend.

4. Test Your App Locally

Before deploying your app, you can test it locally to ensure it's working correctly.

Run the following command in your terminal:

NODE_ENV=dev node app.mjs

5. Deploy your project

Finally, deploy your project. A browser window will open, and you will be prompted to log in to your Genezio account and authorize the CLI to make the deployment. Run the following command in your terminal:

genezio deploy

If your application use environment variables, you can deploy them using the following command:

genezio deploy --env <path-to-your-env-file>
info

You need to deploy your environment variables single time. After that, you can deploy your project without the --env flag.

For more information about environment variables, you can check the official documentation.

Test your app

After deploying your application, you can test it to ensure it's running correctly. To verify that your Express.js app is working, open a web browser and navigate to the URL provided for your deployed function.

This URL can be found in the deployment output under the Functions Deployed section.

Additionally, you can monitor and manage your app through the Genezio App Dashboard. The dashboard URL, also provided after deployment, allows you to access comprehensive views of your project's status and logs.

You can find this URL in the deployment output under the App Dashboard URL section.

Support

We invite you to join our community on Discord for further information and help.

Happy Learning!