Express is a fast, unopinionated, minimalist web framework for Node.js. The Sites’s serverless environment makes it easy to run apps without managing Node.js servers.
Getting Started
Add your Express app to Edgio by running the following command in your project’s root directory:
Bash
1npm i -g @layer0/cli # yarn global add @layer0/cli20 init
Running your app locally
Test your app with the Sites on your local machine by running the following command in your project’s root directory:
Bash
10 dev
Deploying
Deploy your app to the Sites by running the following command in your project’s root directory:
Bash
10 deploy
Overriding the default app location
When you deploy your Express app to the Sites, the Edgio CLI bundles your app as a single javascript file so that it can be run as a serverless function. By default, Edgio looks for your app in the following common locations:
- src/server.ts
- src/server.js
- src/app.ts
- src/app.js
- src/index.ts
- src/index.js
- server.js
- app.js
- index.js
If it cannot find one of these files, you can specify the path to the app in
layer0.config.js
:JavaScript
1const {join} = require('path');23// layer0.config.js4module.exports = {5 connector: '@layer0/express',6 express: {7 appPath: join(process.cwd(), 'path', 'to', 'app.js'),8 },9};
The file you specify in
appPath
should export an instance of an express app using export default
or module.exports
.Serving Static Assets
If your express app serves any static assets, you’ll need to add routes to your Edgio router configuration to serve them from the edge. For example, to serve all paths under
/assets
from dist/client/assets
:JavaScript
1// routes.js2import {Router} from '@layer0/core';34export default new Router()5 // Prevent search engine bot(s) from indexing6 // Read more on: https://docs.layer0.co/applications/cookbook#blocking-search-engine-crawlers7 .noIndexPermalink()8 .match('/assets/:path*', ({cache, serveStatic}) => {9 cache({10 edge: {11 maxAgeSeconds: 60 * 60 * 365, // cache at the edge for one year12 },13 browser: {14 maxAgeSeconds: 60 * 60 * 365, // cache in the browser for one year - only do this if you include hashes in your client asset filenames15 },16 });17 serveStatic('dist/client/assets/:path*');18 })19 .fallback(({renderWithApp}) => renderWithApp()); // serve all unmatched URLs from express
Adding Additional Files Needed during SSR
If your express app expects to be able to read files from the filesystem at runtime, for example an index.html template, you can ensure they are included in the app bundle that is deployed to Sites’s serverless workers by adding the following to layer0.config.js
JavaScript
1module.exports = {2 /* ... */34 includeFiles: {5 // Include index.html in the serverless bundle6 'dist/client/index.html': true,7 },8};
Transpiling and TypeScript support
Edgio will automatically transpile JavaScript and TypeScript source code for running on Node.js version 16. If you want to control how
source files are compiled, you can transpile your app on your own and point your
appPath
config to the transpiled version of your app’s main entry point.Edgio Applications’s support for Node.js version 16 is undergoing end-of-life. View the end-of-life plan.
Bundling Options
By default, Edgio uses ESBuild to transpile and bundle your application code. If you’re having difficulty fitting your app within the limit for serverless bundles, you can try bundling with ncc, which should produce smaller bundles, by adding the following to layer0.config.js:
JavaScript
1module.exports = {2 express: {3 bundler: '@vercel/ncc',4 },5};
Then add ncc to your app’s build dependencies:
1npm i -D @vercel/ncc@^0.34.0
Or, using yarn:
1yarn add --dev @vercel/ncc@^0.34.0
NCC produces a tree-shaken, bundle which includes your application code and all of its dependencies in a single file (written to .layer0/lambda/backends/index.js). NFT is also supported:
JavaScript
1module.exports = {2 express: {3 bundler: '@vercel/nft',4 },5};
Then add nft to your app’s build dependencies:
1npm i -D @vercel/nft@^0.21.0
Or, using yarn:
1yarn add --dev @vercel/nft@^0.21.0
NFT is similar to NCC, but it produces an exploded directory tree instead of including all of your code in a single file. We recommend trying ncc first.