[英]Node express request body undefined on AWS Lambda
Im using AWS Amplify to generate Node Express rest API endpoints.我使用 AWS Amplify 生成 Node Express rest API 端点。
I recently added a new endpoint, but I keep getting undefined in the request body, and I can't figure out where I might have miss-configured my application.我最近添加了一个新端点,但我在请求正文中一直未定义,我无法弄清楚我可能在哪里错误配置了我的应用程序。
App.js应用程序.js
var express = require("express");
var bodyParser = require("body-parser");
var awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");
// declare a new express app
var app = express();
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(awsServerlessExpressMiddleware.eventContext());
// Enable CORS for all methods
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
var aws = require("aws-sdk");
const https = require("https");
app.post("/refresh", function(req, res){
console.log(JSON.stringify(req.body));
// {}
console.log(JSON.parse(req.body));
//Error: unexpected token o in JSON at position 1
});
I'm testing this in the AWS console with this payload我正在使用此有效负载在 AWS 控制台中对此进行测试
{
"path": "/refresh",
"httpMethod": "POST",
"header": "{\"content-Type\":\"application/json\"}",
"body": "{\"Username\":\"test\"}"
}
Set header as将 header 设置为
"Content-Type: application/json"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.