Skip to content

Middleware does not work for root path #128

@jkinghed

Description

@jkinghed

Either I'm missing something in the documentation (very likely), or middleware does not trigger for root paths. I've tried both GET and POST.

Example code

const lambdaApi = require('lambda-api');
const api = lambdaApi({
    base: 'user',
    logger: true
});

api.use((req, res, next) => {
    console.log('MIDDLEWARE START');
    req.myVar = 'YES, HELLO';

    next();
});

//static path
api.get('/hello', async (req, res) => {

    console.log('/hello', req.myVar);
    res.json({hello: req.myVar});

});

//root path
api.get('/', async (req, res) => {

    console.log('root', req.myVar);

    res.json({hello: req.myVar});

});

exports.handler = async (event, context) => {
    console.log('event:', JSON.stringify(event, undefined, 4));
    console.log('context:', JSON.stringify(context, undefined, 4));
    return await api.run(event, context);
};

And logs

image

API Gateway has ANY method for base path /user

Any pointers on what I'm doing wrong?

Activity

self-assigned this
on Feb 3, 2023
added this to the v1.2.0 milestone on Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

    Development

    Participants

    @naorpeled@jkinghed

    Issue actions

      Middleware does not work for root path · Issue #128 · jeremydaly/lambda-api