Koa2 发送 404,但功能有效。这是怎么回事?
Koa2 sends 404, though function works. What’s the deal?
我已经用 Koa2 设置了一个 API,它在 post 时向 mailgun 发送一个请求到 /contact,并附上姓名、电子邮件和消息。
电子邮件发送得很好,但我在控制台客户端收到 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);
});
}
}
当您使用 async
-await
模式时,您的代码应如下所示:
async mail(ctx){
...
var result = await mailgun.messages().send(data)
console.log(body);
};
缺少的部分是await
。由于 - 由于文档 - mailgun-js
模块也实现了承诺,这应该有效(未测试)。
我已经用 Koa2 设置了一个 API,它在 post 时向 mailgun 发送一个请求到 /contact,并附上姓名、电子邮件和消息。 电子邮件发送得很好,但我在控制台客户端收到 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);
});
} }
当您使用 async
-await
模式时,您的代码应如下所示:
async mail(ctx){
...
var result = await mailgun.messages().send(data)
console.log(body);
};
缺少的部分是await
。由于 - 由于文档 - mailgun-js
模块也实现了承诺,这应该有效(未测试)。