Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Minimum reproducible example for "Method not allowed" error

Aperta
#251 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript, node.js
Ambito
api, backend

Direzione di ricerca

Inizia da test.js e riproduci la richiesta GET verso il server locale. Traccia le chiamate api.get('/'), api.routes(true) e api.run({ event, context }) per determinare perché la route GET visualizzata restituisce ancora “Method not allowed”. Il lavoro è completato quando l'esempio minimo gestisce correttamente la richiesta o documenta la causa confermata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 ([email protected])

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  ║
╚══════════╧═════════╧═══════════╝
Lingua principale
JavaScript
Stelle
1.5k
Fork
127
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jeremydaly/lambda-api

Tutte le issue di jeremydaly/lambda-api

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.