[英]Using the Node Request module with AWS Lambda and API Gateway
This lambda function works: 此lambda函数起作用:
exports.handler = function(event, context, callback) {
const done = (err, res, func) => callback(null, {
statusCode: '200',
body: res,
headers: { 'Content-Type': 'application/json' },
})
done(null, 'works'))
}
I can hit the API Gateway end point and receive the response message, 'works'
. 我可以点击API网关端点并接收响应消息
'works'
。
However, when I add a request call ( POST
ing a message to Slack) that looks like this: 然而,当我加入请求调用(
POST
荷兰国际集团的消息懈怠)看起来像这样的:
const request = require('request')
const moment = require('moment-timezone')
exports.handler = function(event, context, callback) {
// the json object is built following these docs:
// https://api.slack.com/docs/message-attachments
const SLACK_WEB_HOOK_URL = process.env.SLACK_WEB_HOOK_URL
const uri = SLACK_WEB_HOOK_URL
const method = 'POST'
const options = {
json,
uri,
method,
}
const slack = (opts) => {
request.post(opts, (err, response, body) => {
if (response.statusCode < 300) {
return 'Success!'
} else {
return 'Err!'
}
})
}
const done = (err, res, func) => callback(null, {
statusCode: '200',
body: res,
headers: { 'Content-Type': 'application/json' },
})
done(null, slack(options))
}
The server hangs when I hit the API end point ... 当我到达API端点时,服务器挂起...
And I get a Task timed out after 10.00 seconds
error: 我得到一个
Task timed out after 10.00 seconds
错误Task timed out after 10.00 seconds
:
{
"errorMessage": "2017-06-23T16:41:21.483Z c385e32e-5832-11e7-8c4f-673b524cf783 Task timed out after 10.00 seconds"
}
But my Slack POST
request gets sent and I see the message in the channel. 但是我的Slack
POST
请求被发送了,我在频道中看到了消息。
How do I send the POST
request and then wait for it to return, and then exit the lambda function with a custom response message? 如何发送
POST
请求,然后等待它返回,然后使用自定义响应消息退出lambda函数?
Thanks for the help! 谢谢您的帮助!
Put the callback into the callback of the post
method 将回调放入
post
方法的回调中
request.post(opts, (err, response, body) => {
if (response.statusCode < 300) {
return callback(null, {
statusCode: '200',
body: res,
headers: { 'Content-Type': 'application/json' },
});
} else {
return callback(err);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.