修改处理程序在使用无服务器框架时请求某些端点时返回响应主体
我正在使用无服务器框架在AWS lambda上部署整个Nodejs Type-Script应用程序。
当请求端点时,它调用了lambda函数,然后调用与该端点相对应的功能并执行。我敢肯定,lambda能够达到该功能,因为我可以看到CloudWatch日志中该功能中存在的控制台日志。
从我的后端代码中,Lambda收到了状态代码200,但没有返回响应主体。
我的无服务器文件,
functions:
index:
handler: src/index.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
这是我的处理程序index.ts
let databaseconnect = async() =>{
if(!DatabaseService.isConnected)
await DatabaseService.getConnection()
...
...
...
const server = awsServerlessExpress.createServer(app)
return server
}
exports.handler = async(event: any, context: any) => {
let server = await databaseconnect()
awsServerlessExpress.proxy(server, event, context)
}
我的路由器,
router.get('/check',(req:Request,res: Response)=>{
return res.status(200).json({message: "Its working one."});
});
// //router.get("/abc",abc)
router.post("/abc",Controller.abc)
我的控制器,
...
...
...
public static abc = async (req:Request,res:Response,next:any) => {
console.log("ABC controller")
return res.status(200).json({message:JSON.stringify("get request")})
}
}
export default Controller;
上述控制台日志在CloudWatch
日志上可见,
[32minfo[39m: POST /api/v1/user/abc 200 0ms
{
"meta": {
"req": {
"headers": {
"connection": "close",
"content-length": "2",
"host": "localhost",
"x-apigateway-context": "%7B%22callbackWaitsForEmptyEventLoop%22%3Atrue%2C%22functionVersion%22%3A%22%24LATEST%22%2C%22functionName%22%3A%22lambda-lam-test-index%22%2C%22memoryLimitInMB%22%3A%224096%22%2C%22logGroupName%22%3A%22%2Faws%2Flambda%2Flambda-lam-test-index%22%2C%22logStreamName%22%3A%222022%2F06%2F21%2F%5B%24LATEST%5Deb2438ba18024c8a8f7457ae221b0cee%22%2C%22invokedFunctionArn%22%3A%22arn%3Aaws%3Alambda%3Aap-south-1%3A945159732863%3Afunction%3Alambda-lam-test-index%22%2C%22awsRequestId%22%3A%22865999b5-d830-45cb-bd3e-4dc56702c5eb%22%7D",
"x-apigateway-event": "%7B%22resource%22%3A%22%2F%7Bproxy%2B%7D%22api%2Fv1%2Fuser%2Fabc%22%2C%22httpMethod%ltiValueHeaders%22%3Anull%2C%22queryStringParameters%22%3Anull%2C%22multiValueQueryStringParameters%22%3Anull%2C%22pathParameters%22%3A%7B%22proxy%22%3A%23A%2221%2FJun%2F2022%3A14%3A28%3A50%20%2B0000%22%2C%22path%22%3A%22%2F%7Bproxy%2B%7D%22%2C%22accountId%22%3A%22945159732863%22%2C%22protocol%22%3A%22HTTP%2F1.1%22%2C%22stage%22%3A%22test-invoke-stage%22%2C%22domainPrefix%22%3A%22testPrefix%22%2C%22requestTimeEpoch%22%3A1655821730840%2C%22requestId%22%3A%22433bc178-8a53-47b7-9ca5-2a6cf8b31c02%22%2C%22identity%22%3A%7B%22cognitoIdentityPoolId%22%3Anull%2C%22cognitoIdentityId%22%3Anull%2C%22apiKey%22%3A%22test-invoke-api-key%22%2C%22principalOrgId%22%3Anull%2C%22cognitoAuthenticationType%22%3Anull%2C%22userArn%22%3A%22arn%3Aaws%3Aiam%3A%3A945159732863%3Auser%2Fharsh%22%2C%22apiKeyId%22%3A%22test-invoke-api-key-id%22%2C%22userAgent%22%3A%22aws-internal%2F3%20aws-sdk-java%2F1.12.201%20Linux%2F5.4.186-113.361.amznlomainName%22%3A%22testPrefix.testDomainName%22%2C%22apiId%22%3A%22vzfqx2hdx8%22%7D%2C%22isBase64Encoded%22%3Afalse%7D"
},
"httpVersion": "1.1",
"method": "POST",
"originalUrl": "/api/v1/user/abc",
"query": {},
"url": "/api/v1/user/abc"
},
"res": {
"statusCode": 200
},
"responseTime": 0
}
}
.........8D5999b5-d8F0-45cF-FWA3e-4dc5WFA2c5eb INFO ABC controller
应进行哪些修改以返回响应主体以及与状态代码?
I am using serverless framework to deploy entire nodejs type-script app on AWS Lambda.
When an endpoint is requested it invokes the lambda function, after which function corresponding to that endpoint is called and gets executed. I am certain that the lambda is able to get to that function as I can see the console log present in that function in CloudWatch logs.
From my backend code, lambda receives the statuscode 200 but does not returns the response body.
My serverless file,
functions:
index:
handler: src/index.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
This is my handler which is in index.ts
let databaseconnect = async() =>{
if(!DatabaseService.isConnected)
await DatabaseService.getConnection()
...
...
...
const server = awsServerlessExpress.createServer(app)
return server
}
exports.handler = async(event: any, context: any) => {
let server = await databaseconnect()
awsServerlessExpress.proxy(server, event, context)
}
My router,
router.get('/check',(req:Request,res: Response)=>{
return res.status(200).json({message: "Its working one."});
});
// //router.get("/abc",abc)
router.post("/abc",Controller.abc)
My controller,
...
...
...
public static abc = async (req:Request,res:Response,next:any) => {
console.log("ABC controller")
return res.status(200).json({message:JSON.stringify("get request")})
}
}
export default Controller;
The mentioned console log is visible on CloudWatch logs
logs,
[32minfo[39m: POST /api/v1/user/abc 200 0ms
{
"meta": {
"req": {
"headers": {
"connection": "close",
"content-length": "2",
"host": "localhost",
"x-apigateway-context": "%7B%22callbackWaitsForEmptyEventLoop%22%3Atrue%2C%22functionVersion%22%3A%22%24LATEST%22%2C%22functionName%22%3A%22lambda-lam-test-index%22%2C%22memoryLimitInMB%22%3A%224096%22%2C%22logGroupName%22%3A%22%2Faws%2Flambda%2Flambda-lam-test-index%22%2C%22logStreamName%22%3A%222022%2F06%2F21%2F%5B%24LATEST%5Deb2438ba18024c8a8f7457ae221b0cee%22%2C%22invokedFunctionArn%22%3A%22arn%3Aaws%3Alambda%3Aap-south-1%3A945159732863%3Afunction%3Alambda-lam-test-index%22%2C%22awsRequestId%22%3A%22865999b5-d830-45cb-bd3e-4dc56702c5eb%22%7D",
"x-apigateway-event": "%7B%22resource%22%3A%22%2F%7Bproxy%2B%7D%22api%2Fv1%2Fuser%2Fabc%22%2C%22httpMethod%ltiValueHeaders%22%3Anull%2C%22queryStringParameters%22%3Anull%2C%22multiValueQueryStringParameters%22%3Anull%2C%22pathParameters%22%3A%7B%22proxy%22%3A%23A%2221%2FJun%2F2022%3A14%3A28%3A50%20%2B0000%22%2C%22path%22%3A%22%2F%7Bproxy%2B%7D%22%2C%22accountId%22%3A%22945159732863%22%2C%22protocol%22%3A%22HTTP%2F1.1%22%2C%22stage%22%3A%22test-invoke-stage%22%2C%22domainPrefix%22%3A%22testPrefix%22%2C%22requestTimeEpoch%22%3A1655821730840%2C%22requestId%22%3A%22433bc178-8a53-47b7-9ca5-2a6cf8b31c02%22%2C%22identity%22%3A%7B%22cognitoIdentityPoolId%22%3Anull%2C%22cognitoIdentityId%22%3Anull%2C%22apiKey%22%3A%22test-invoke-api-key%22%2C%22principalOrgId%22%3Anull%2C%22cognitoAuthenticationType%22%3Anull%2C%22userArn%22%3A%22arn%3Aaws%3Aiam%3A%3A945159732863%3Auser%2Fharsh%22%2C%22apiKeyId%22%3A%22test-invoke-api-key-id%22%2C%22userAgent%22%3A%22aws-internal%2F3%20aws-sdk-java%2F1.12.201%20Linux%2F5.4.186-113.361.amznlomainName%22%3A%22testPrefix.testDomainName%22%2C%22apiId%22%3A%22vzfqx2hdx8%22%7D%2C%22isBase64Encoded%22%3Afalse%7D"
},
"httpVersion": "1.1",
"method": "POST",
"originalUrl": "/api/v1/user/abc",
"query": {},
"url": "/api/v1/user/abc"
},
"res": {
"statusCode": 200
},
"responseTime": 0
}
}
.........8D5999b5-d8F0-45cF-FWA3e-4dc5WFA2c5eb INFO ABC controller
What modification should be made to return the response body along with the statuscode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论