从 api 调用中表达 lambda return JSON
Express lambda return JSON from api call
我有一个无服务器 Express 应用程序。在应用程序中,我有一个名为“/”的 app.get,它应该调用 api,从 api 检索数据并将其发送回用户。
https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev
我可以在返回的页面上看到 json 的数据。
这是我的index.js的lambda函数:
const serverless = require('serverless-http');
const express = require('express');
const request = require('request');
const app = express()
app.get('/', function (req, res) {
var options = { method: 'POST',
url: 'https://some.api.domain/getTopNstc',
headers:
{ 'Content-Type': 'application/json' },
body: {},
json: true
};
request(options, function (error, response, body) {
console.log('request call')
if (error) throw new Error(error);
// res.status(200).send(response);
res.json(response);
});
});
module.exports.handler = serverless(app);
但是我可以通过 axios(或其他 promise-request 库)调用 lambda '/'
我尝试使用以下代码调用我的 lambda:
axios.get('https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev', {
headers: {
'Content-Type': 'application/json',
},
body:{}
}).then((res) => {
console.log(res);
});
Failed to load
https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'myDomain' is therefore not
allowed access. bundle.js:31 Cross-Origin Read Blocking (CORB) blocked
cross-origin response
https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev with MIME
type application/json. See
https://www.chromestatus.com/feature/5629709824032768 for more
details.
Api 网关配置:
我同意@KMo。很确定这是一个 CORS 问题。 npm 中有一个模块正是用于此目的,请仔细阅读 here.
要安装它,运行 npm install -s cors
然后在您的 Express 应用中,添加以下内容:
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());
我有一个无服务器 Express 应用程序。在应用程序中,我有一个名为“/”的 app.get,它应该调用 api,从 api 检索数据并将其发送回用户。
https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev
我可以在返回的页面上看到 json 的数据。
这是我的index.js的lambda函数:
const serverless = require('serverless-http');
const express = require('express');
const request = require('request');
const app = express()
app.get('/', function (req, res) {
var options = { method: 'POST',
url: 'https://some.api.domain/getTopNstc',
headers:
{ 'Content-Type': 'application/json' },
body: {},
json: true
};
request(options, function (error, response, body) {
console.log('request call')
if (error) throw new Error(error);
// res.status(200).send(response);
res.json(response);
});
});
module.exports.handler = serverless(app);
但是我可以通过 axios(或其他 promise-request 库)调用 lambda '/'
我尝试使用以下代码调用我的 lambda:
axios.get('https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev', {
headers: {
'Content-Type': 'application/json',
},
body:{}
}).then((res) => {
console.log(res);
});
Failed to load https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'myDomain' is therefore not allowed access. bundle.js:31 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://y31q4zn654.execute-api.eu-west-1.amazonaws.com/dev with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Api 网关配置:
我同意@KMo。很确定这是一个 CORS 问题。 npm 中有一个模块正是用于此目的,请仔细阅读 here.
要安装它,运行 npm install -s cors
然后在您的 Express 应用中,添加以下内容:
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());