我已经设置了一个使用Koa2设置的API,它会在发送到/联系姓名,电子邮件和消息时向mailgun发送请求。 电子邮件发送得很好,但我在控制台客户端获得了404。为什么我会这样做?
module.exports = {
/**
*@api {post} /contact
*@apiGroup Public
* @apiName sendMail
* @apiParam {String} [name] User needs to provide their name
* @apiParam {String} [email] User needs to provide their email
* @apiParam {String} [message] user needs to provide their message
* @apiParamExample {String} Request Params :
* {
* "name" : "Cello",
* "email" : "yo@yo.ma,
* "message" : "you are so great"
* }
* @apiSuccess {Object} return value A success or failure
* @apiSuccessExample {json} Response:
* {
* "result" : "success"
* "callback" : "Do something"
* }
* @apiExample {curl} Example usage:
* curl -i http://localhost:4000/contact
* @apiDescription Any user can submit contact
* @apiHeader {String} Authorization JWT Authorization header (optional)
* @apiHeaderExample {json} Request Authorization Header
* {
* "authorization" : "jkahdkjashdk324324342"
* }
*/
async mail(ctx){
let {email, name, message} = ctx.request.body;
if (!email) {
ctx.throw(400, 'please provide the email')
}
if (!name) {
ctx.throw(400, 'please provide a name')
}
if (!message) {
ctx.throw(400, 'please provide a message')
}
var api_key = 'key-xxxxxxxxxx';
var domain = 'mg.domain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
var data = {
from: name+'<'+email+'>',
to: 'me@domain.com',
subject: 'Contact Form',
text: message
};
var result = mailgun.messages().send(data, function (error, body) {
console.log(body);
});
} }
答案 0 :(得分:0)
当您使用async
- await
模式时,您的代码应如下所示:
async mail(ctx){
...
var result = await mailgun.messages().send(data)
console.log(body);
};
缺少的部分是await
。由于文档 - mailgun-js
模块也实现了承诺,这应该有效(未经过测试)。