【发布时间】:2020-01-22 18:04:21
【问题描述】:
使用nuxt 和http 请求axios,这里是配置:
nuxt.config.js
axios: {
proxy: true,
https: true,
prefix: '/api'
}
form.vue
this.$axios.$post('/mail', {
...
}
提交表单后,请求url为:
http://localhost:3000/api/mail
但是:
POST http://localhost:3000/api/mail 404(不是 找到)
在我的项目中:
/api
- index.js
- /routes
-- /mail.js
index.js:
const express = require('express')
var mail = require('./routes/mail.js')
mail.js
var router = express.Router()
router.post('/', function(req, res, next) {
var mailInfo = req.body
var mailDetails = getMailDetails(mailInfo.type, mailInfo.body)
var mailOptions = {
priority: 'high',
from: process.env.SENDGRID_FROM || 'contact@test.com',
to: mailInfo.to || mailDetails.to,
subject: mailDetails.subject,
html: mailDetails.msg
}
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error)
res.status(404).json(error)
} else {
// console.log('Email sent: ' + info.response);
res.status(200).json({
message: 'email sent successfully'
})
}
})
})
module.exports = router
有什么想法吗? ..
【问题讨论】:
-
我很惊讶你得到 404,考虑到你没有在任何地方听,
./routes/mail.js中的内容是什么?因为 index.js 中的代码没有设置路由,也没有做很多 -
没错,你没有设置监听你的 axios 请求的服务器。您的客户端代码似乎工作正常。
标签: javascript node.js vue.js axios nuxt.js