jeremydaly / jeremydaly/lambda-api

Minimum reproducible example for "Method not allowed" error

Open
#251 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.5k
Forks
127
Avg merge
31m
Merged PRs (30d)
2

Description

I have this MRE

// test.js
import http from 'http';
import getLambdaAPI from 'lambda-api';

const getLambdaEvent = req => {
  const event = {
    path: req.url,
    httpMethod: req.method,
    requestContext: {
      http: { method: req.method, path: req.url },
    },
  };

  return event;
};

const api = getLambdaAPI();

api.get('/', (req, res) => {
    const SERVICE_RESPONSE = "API_RUNNING";
    console.log({ SERVICE_RESPONSE });
    //return { SERVICE_RESPONSE: "API_RUNNING" };
    res.status(200).json({ SERVICE_RESPONSE: "API_RUNNING" });
});

const lambdaHandler = async (event, context) => {
    console.log(event);
    console.log({
      ROUTE_CODE: 'LAMBDA_EVENT',
      PATH: event.requestContext?.http?.path,
      METHOD: event.requestContext?.http?.method,
    });

    api.routes(true);
    return await api.run({ event, context });
  };

const handler = async (req, res) => {
  try {

    const lambdaEvent = getLambdaEvent(req);
    const lambdaRes = await lambdaHandler(lambdaEvent, null)

    res.statusCode = lambdaRes.statusCode;
    res.end(lambdaRes.body);

  } catch(SERVER_ERROR) {
    console.log({ SERVER_ERROR });
    res.statusCode = 500;
    res.end(JSON.stringify({ REQUEST_PARSING_ERROR: SERVER_ERROR.toString() }));
  }
}

const server = http.createServer(handler);
server.listen(8080);

When I run it with node test.js and make a GET request with postman to http://localhost:8080 I get {"error":"Method not allowed"}

Can you spot the issue? Me not neither GPT-4 (lambda-api@1.03)

The console says this

api$ node test.js
{
  path: '/',
  httpMethod: 'GET',
  requestContext: { http: { method: 'GET', path: '/' } }
}
{ ROUTE_CODE: 'LAMBDA_EVENT', PATH: '/', METHOD: 'GET' }
╔══════════╤═════════╤═══════════╗
║  METHOD  │  ROUTE  │  STACK    ║
╟──────────┼─────────┼───────────╢
║  GET     │  /      │  unnamed  ║
╚══════════╧═════════╧═══════════╝

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with test.js and reproduce the GET request against the local server. Trace the api.get('/'), api.routes(true), and api.run({ event, context }) calls to determine why the displayed GET route still returns “Method not allowed.” Done means the minimal example handles the request successfully or documents the confirmed cause.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.