I’m trying to handle all routes from a single endpoint (aws lambda) using aws-serverless-express, but so far I only managed to catch one endpoint. How can I catch them all?
import next from 'next';
import es from 'aws-serverless-express';
// import awsServerlessExpressMiddleware from 'aws-serverless-express/middleware' // TODO Don't know how to use it neither what it may help for
import { failure } from "../../utils/browserResponse";
import { isHostedOnAWS } from "../../utils/aws";
// XXX next.dev enables HMR, which we don't want if not in development mode, or when we are on AWS's infrastructure
const app = next({ dev: !isHostedOnAWS() && process.env.NODE_ENV === 'development' });
const handle = app.getRequestHandler();
const server = es.createServer((req, res) => {
console.log('req from', req.headers.host);
handle(req, res);
});
export async function handler(event, context, callback) {
try {
return es.proxy(server, event, context);
} catch (e) {
callback(null, await failure({ status: false }, e));
}
}
service:
name: serverless-with-next
frameworkVersion: ">=1.26.1"
plugins:
- serverless-webpack
- serverless-offline
- serverless-plugin-simulate
- serverless-jest-plugin
- serverless-domain-manager
# Enable auto-packing of external modules
# See https://serverless-stack.com/chapters/add-support-for-es6-es7-javascript.html
custom:
webpackIncludeModules: true
# webpackIncludeModules:
# forceInclude:
# - next
domains:
development: 'swn.dev.vadorequest.fr'
memorySizes: # TODO Check how much is actually needed
development: 512
customDomain:
domainName: ${self:custom.domains.${self:provider.stage}}
# basePath: '' # This will be prefixed to all routes
stage: ${self:provider.stage}
createRoute53Record: true
simulate:
dist: docker
# services:
# file: docker-compose.yml
# projectName: serverless-with-next
provider:
name: aws
runtime: nodejs6.10
timeout: 30
stage: ${opt:stage, 'development'}
region: ${opt:region, 'us-east-1'}
memorySize: ${self:custom.memorySizes.${self:provider.stage}}
environment:
NODE_ENV: ${self:provider.stage}
package:
individually: true
functions:
server:
handler: src/functions/server/server.handler
events:
- http:
method: GET
path: /
- http:
method: GET
path: /_next/{proxy+}
status:
handler: src/functions/status/status.handler
events:
- http:
method: get
path: status
This is likely a misconfiguration of aws-serverless-express or serverless.yml but I can’t figure it out.