Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receiving null from lambda #270

Closed
matttm opened this issue Feb 19, 2023 · 5 comments
Closed

Receiving null from lambda #270

matttm opened this issue Feb 19, 2023 · 5 comments

Comments

@matttm
Copy link

matttm commented Feb 19, 2023

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
Copy link

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

@whenmoon
Copy link

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

@emyriounis
Copy link

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

@akshay-nm
Copy link

akshay-nm commented Mar 8, 2024

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
Copy link

akshay-nm commented Mar 8, 2024

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 
}

@matttm matttm closed this as completed Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants