Serverless Framework Express Rest 上的 502 Bad Gateway 错误-API
502 Bad Gateway Error on Serverless Framework Express Rest-API
我正在尝试使用无服务器框架构建一个 express rest-api,代码如下。我有一个指向路径 /fruits
的有效 POST
请求方法,但以下 GET
请求方法会引发 502 Bad Gateway 错误。
const serverless = require('serverless-http');
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
...
const dynamoDB = new AWS.DynamoDB.DocumentClient();
app.get('/fruits/:fruitName', (req, res) => {
const params = {
TableName: TABLE_NAME,
Key: {
fruitName: req.params.fruitName,
},
}
dynamoDB.get(params, (err, res) => {
if (err) {
console.log(error);
res.status(400).json({ error: 'Could not get fruit' });
}
if (res.Item) {
const { fruitName, biName} = res.Item;
res.json({ fruitName, biName});
} else {
res.status(404).json({ error: "Fruit not found" });
}
})
})
...
module.exports.handler = serverless(app);
我设置了一个serverless.yml如下
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-west-2
iamRoleStatements:
- Effect: 'Allow'
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- { "Fn::GetAtt": ["FruitsTable", "Arn" ] }
environment:
TABLE_NAME: 'fruits'
resources:
Resources:
FruitsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
- AttributeName: fruitName
AttributeType: S
KeySchema:
- AttributeName: fruitName
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: 'fruits'
functions:
app:
handler: index.handler
events:
- httpApi: 'GET /fruits/{fruitName}'
- httpApi: 'POST /fruits'
非常感谢任何帮助。
问题是相同的变量命名导致覆盖。以下将解决这个问题。
app.get('/fruits/:fruitName', (req, res) => {
const params = {
TableName: TABLE_NAME,
Key: {
fruitName: req.params.fruitName,
},
}
dynamoDB.get(params, (err, result) => {
if (err) {
console.log(error);
res.status(400).json({ error: 'Could not get fruit' });
}
if (result.Item) {
const { fruitName, biName} = result.Item;
res.json({ fruitName, biName});
} else {
res.status(404).json({ error: "Fruit not found" });
}
})
})
我正在尝试使用无服务器框架构建一个 express rest-api,代码如下。我有一个指向路径 /fruits
的有效 POST
请求方法,但以下 GET
请求方法会引发 502 Bad Gateway 错误。
const serverless = require('serverless-http');
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
...
const dynamoDB = new AWS.DynamoDB.DocumentClient();
app.get('/fruits/:fruitName', (req, res) => {
const params = {
TableName: TABLE_NAME,
Key: {
fruitName: req.params.fruitName,
},
}
dynamoDB.get(params, (err, res) => {
if (err) {
console.log(error);
res.status(400).json({ error: 'Could not get fruit' });
}
if (res.Item) {
const { fruitName, biName} = res.Item;
res.json({ fruitName, biName});
} else {
res.status(404).json({ error: "Fruit not found" });
}
})
})
...
module.exports.handler = serverless(app);
我设置了一个serverless.yml如下
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-west-2
iamRoleStatements:
- Effect: 'Allow'
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- { "Fn::GetAtt": ["FruitsTable", "Arn" ] }
environment:
TABLE_NAME: 'fruits'
resources:
Resources:
FruitsTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
- AttributeName: fruitName
AttributeType: S
KeySchema:
- AttributeName: fruitName
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: 'fruits'
functions:
app:
handler: index.handler
events:
- httpApi: 'GET /fruits/{fruitName}'
- httpApi: 'POST /fruits'
非常感谢任何帮助。
问题是相同的变量命名导致覆盖。以下将解决这个问题。
app.get('/fruits/:fruitName', (req, res) => {
const params = {
TableName: TABLE_NAME,
Key: {
fruitName: req.params.fruitName,
},
}
dynamoDB.get(params, (err, result) => {
if (err) {
console.log(error);
res.status(400).json({ error: 'Could not get fruit' });
}
if (result.Item) {
const { fruitName, biName} = result.Item;
res.json({ fruitName, biName});
} else {
res.status(404).json({ error: "Fruit not found" });
}
})
})