Hi –
I’ve got a very nice web app put together using serverless and AWS, using express to handle different routes.
Everything works fine, except setting up a “catch-all” route to handle bad URLs.
I’ve found one side, which is to set up the final route in my “serverless.yml” file as:
# ... other routes ...
catch_all:
handler: index.handler
events:
- http:
path: /{any+}
method: any
The part I can’t seem to get to work is the node side of things. The (possibly) important bits of my index.ts
file:
const serverless = require('serverless-http')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const AWS = require('aws-sdk')
// ...
app.use(bodyParser.json({ strict: false }))
// Get User endpoint
app.get('/users', getUserName(dynamoDb))
// ... other route handlers ...
// Catch-all endpoint
app.all(/.*/, function(req, res) {
res.redirect('/')
})
For the app.all
first argument, I’ve used /.*/
, '.*'
, '*'
, '/*'
, and ‘/{any+}’.
When I put in a URL that isn’t otherwise handled, the browser comes back with an empty page, and the catch_all
route is never activated (according to CloudWatch logs).
Any help much appreciated!