A
A
Andrew2021-12-25 15:03:34
Amazon Web Services
Andrew, 2021-12-25 15:03:34

How to process all requests directed to API Gateway through one Lambda function?

I created a lambda function which has code like this:

'use strict'

const { createServer, proxy } = require('aws-serverless-express')
const express = require('express')


const app = express()

app.use(express.urlencoded({ extended: true }))
app.use(express.json())


app.get('/', (req, res) => {
  res.send('ALOHA HONUA!')
})

app.get('/products', async (req, res) => {
  res.send('Товары')
})


app.get('/categories', async (req, res) => {
  res.send('Категории')
})

module.exports.handler = (event, ctx) => {
  proxy(server, event, ctx)
}


API Gateway configured in this way:
61c707f61a943795674377.png

I.e. all requests for this API lead to one function. In the logs, I see that the function is called, however, regardless of the endpoint, only the handler for get('/') is processed. If I delete it, I get a Cannot GET /.

I believe that the problem is in the configuration of the express itself, but I do not understand how to fix it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey, 2021-12-27
@AndrewRusinas

In general, the aws-serverless-express package is no longer very relevant and turned out to be. It may have been configurable to make it work, but their npm page now points to a slightly different github repository.
It is enough to follow one of their examples, for example this one, and everything will work like clockwork.
It is very convenient for various kinds of knick-knacks, if you are too lazy to fool around with dockers / nginxes.

E
evnuh, 2015-08-20
@Azurre

https://code.google.com/p/crypto-js/

L
Lynn "Coffee Man", 2015-08-21
@Lynn

Choose cryptojs.altervista.org/publickey.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question