使用nuxt
并针对http请求axios
,这是配置:
nuxt.config.js
axios: {
proxy: true,
https: true,
prefix: '/api'
}
form.vue
this.$axios.$post('/mail', {
...
}
提交表单后,请求网址为:
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
有什么主意吗? ..
答案 0 :(得分:0)
您是否指定了要作为代理服务器访问的服务器的地址?
// nuxt.config.js
proxy: {
'prefix': 'url'
}