Express on Netlify
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
# Key features
These features provide important benefits for Express apps, including ones built and deployed with Netlify.
- Routing. Express provides a robust routing API that allows you to perform complex routing tasks. With this API, you can split code on a per-route basis and not be strictly limited by Netlify’s file-based routing for Functions.
- Middleware. Express provides a comprehensive set of HTTP utility methods and middleware that you can use to develop a robust API for your application.
# Netlify integration
Express apps on Netlify work because of Netlify Functions.
When you deploy an Express app on Netlify as serverless functions, you get to leverage all of the benefits of running on-demand, server-side code without having to run a dedicated server.
# Deploy an Express app on Netlify
This section demonstrates how to deploy an Express project on Netlify — either alongside a frontend app that uses another framework, or as a standalone Express app.
# Use Express with a frontend app
Before you begin, make sure you have Node.js version 16.0.0 or later installed on your machine. Then, you can start a new project using Express.
Install the following dependencies. If your Express project uses JavaScript, you can exclude
@netlify/functions
and@types/express
.npm i express serverless-http @netlify/functions @types/express
Create a Netlify Function file for either TypeScript or JavaScript. For example, you might create a function file called
api.ts
.In the newly created file, write the function that will import Express and create a router object to handle routing. Here is an example:
// YOUR_BASE_DIRECTORY/netlify/functions/api.ts import express, { Router } from 'express'; import serverless from 'serverless-http'; const api = express(); const router = Router(); router.get('/hello', (req, res) => res.send('Hello World!')); api.use('/api/', router); export const handler = serverless(api);
Add the following configuration in your
netlify.toml
:[functions] external_node_modules = ["express"] node_bundler = "esbuild" [[redirects]] force = true from = "/api/*" status = 200 to = "/.netlify/functions/api/:splat"
In the
redirects
section is a rewrite that enables Express to parse the URLs configured in your function file. In this example, Express can now successfully parse/api
, which we configured above withapi.use('/api/', router);
.If you don’t wish to add the rewrite, you might have to change the configuration in your function to specify the functions path, such as
api.use('/.netlify/functions/', router);
.You can now use these routes in your frontend code. In the above example, you can access the
/hello
route at/api/hello
and any other route that you might add at/api/<slug>
.
# Use Express without a frontend
It’s possible to deploy Express apps on Netlify without a frontend. If you do this, you can access the routes from other frontend apps deployed separately, just as you would with any other API endpoints. In this case, you might have to configure CORS to allow access to the routes from other domains.
To deploy an Express app without a frontend:
- Follow the steps documented above to install Express, create a Netlify Function with your routing code, and create a
netlify.toml
file to register the function and redirects required. - In the
netlify.toml
or the Netlify UI, set a placeholder build command to ensure Netlify builds your functions. For example, the command could beecho Building Functions
.
# Limitations
- Since Express apps are deployed as Netlify Functions, all of the function limitations apply. This includes execution and memory limits.
- It is not recommended to deploy Express apps as background or scheduled functions.
# More resources
Did you find this doc useful?
Your feedback helps us improve our docs.