dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.71k stars 164 forks source link

Receiving null from lambda #270

Closed matttm closed 3 months ago

matttm commented 1 year ago

I am just trying to run express on a lambda and I keep getting null returned.

Do I need an API gateway for this to work?

// app.js
const express = require('express');
const v1 = require('./versions/v1/v1.routes');

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

app.use('/api/v1', v1);

app.get('/', async (req, res) => {
    console.log('the code is being hit');
    return res.json({ lambda: true });
});

module.exports = app;

Lambda code

// index.js
const app = require('app');
const serverless = require('serverless-http');

const handler = serverless(app, { provider: 'aws' });
module.exports.handler = async (context, req) => {
    const tmp = await handler(context, req);
    console.log('tmp', tmp);
    context.res = tmp;
}

Test event logs

Test Event Name
time

Response
null

Function Logs
START RequestId: 5a562aa1-bdfb-459c-8794-fcfaf8c775fc Version: $LATEST
2023-02-19T13:08:21.195Z    5a562aa1-bdfb-459c-8794-fcfaf8c775fc    INFO    the code is being hit
2023-02-19T13:08:21.276Z    5a562aa1-bdfb-459c-8794-fcfaf8c775fc    INFO    tmp {
  statusCode: 200,
  headers: {
    'x-powered-by': 'Express',
    'content-type': 'application/json; charset=utf-8',
    'content-length': '15',
    etag: 'W/"f-CoGLJRqFBGAO5E78DxCjgBIQ/g4"'
  },
  isBase64Encoded: false,
  body: '{"lambda":true}'
}
END RequestId: 5a562aa1-bdfb-459c-8794-fcfaf8c775fc
REPORT RequestId: 5a562aa1-bdfb-459c-8794-fcfaf8c775fc  Duration: 170.34 ms Billed Duration: 171 ms Memory Size: 128 MB Max Memory Used: 73 MB  Init Duration: 289.97 ms

Request ID
5a562aa1-bdfb-459c-8794-fcfaf8c775fc

dependencies

  "dependencies": {
    "express": "^4.18.2",
    "serverless-http": "^3.1.1"
  }
akshay-nm commented 1 year ago

I am also receiving null as a response from my aws lambda. The express app is not returning null.

whenmoon commented 6 months ago

Yes, I am also experiencing this. Express app works fine locally, but routes return null when deployed

emyriounis commented 4 months ago

It happened to me, too. When I switch from nodejs16.x to nodejs18.x

akshay-nm commented 4 months ago

guys, this is still an issue i am facing I checked the logs, my express code is running as i can see logs for the lambda function but api gateway just returns 200 status code with null, no matter what my lambda does

versions:

        "serverless-http": "^3.2.0",
        "express": "^4.18.2",
        "node": "20.6.1",

is there anything I can do to help?

akshay-nm commented 4 months ago

Fixed this!!

@matttm you need to return the res from your handler. doing context.res = tmp doesn't cut it. you need to return tmp cc @whenmoon

module.exports.handler = async (context, req) => {
    const tmp = await handler(context, req);
    console.log('tmp', tmp);
    // context.res = tmp;
    return tmp 
}