dougmoscrop / serverless-http

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

res.setHeader('Access-Control-Allow-Origin', '*'); #53

Closed denes closed 5 years ago

denes commented 6 years ago

How I do to works in all project replace headers response:, I try:

module.exports.handler = serverless(app, { response: function (response, event, context) { // Not working response.headers['Access-Control-Allow-Origin'] = '*'; } });

But, if I do for each function it works:

function (req, res) {

res.setHeader('Access-Control-Allow-Origin', '*');

res.status(200).json('OK');

}

Can you help me?

bsdkurt commented 6 years ago

Why not use the cors middleware? https://github.com/expressjs/cors

This might work too but the cors middleware is the generally accepted solution. -response.headers['Access-Control-Allow-Origin'] = ''; +response.setHeader('Access-Control-Allow-Origin', '');

denes commented 6 years ago

Is not working using cors();

I have try too response.setHeader('Access-Control-Allow-Origin', '*'); but is not working.

dougmoscrop commented 5 years ago

The right way to solve this is to use middleware, like

app.use(async (req, res, next) => {
  await next();
  res.setHeader('Access-Control-Allow-Origin', '*');
});

However, the response transform should let you set a header, I guess, so I fixed it in 1.7.1

anshul7negi commented 5 years ago

@denes did you find a solution?