I am new to Nuxt.js and Node.js. I would like to run a very simple contact form on a 'contact.vue' page. It works very well locally, but as soon as I do an 'npm run generate' to generate the files and upload all of this to an FTP it doesn't work anymore. I have an error in the console: "POST http://website.com/api/message 404 (Not Found)"
I use the POST method on my form with the action that points to 'api / message'. I am using axios in a method (on this same 'contact.vue' page :
async onSubmit (e) {
e.preventDefault()
await this.$axios.post('/api/message', {
name: this.contactForm.name,
firstname: this.contactForm.firstname,
})
.then((res) => {
// On clear le formulaire
this.contactForm.name = ''
this.contactForm.firstname = ''
})
.catch((err) => {
console.log(err)
})
}
I have in the root folder an 'api/' folder with inside 'index.js' and the code :
const app = require('express')()
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
module.exports = { path: '/api', handler: app }
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.post('/message', async (req, res) => {
const contenuFormulaireContact = `
<ul>
<li>Nom : ${req.body.name}</li>
<li>Prénom : ${req.body.firstname}</li>
</ul>
`
// NODEMAILER
const transporter = nodemailer.createTransport({
host: '',
port: 465,
secure: true,
auth: {
user: '',
pass: ''
}
})
const info = await transporter.sendMail({
from: '"Website's Name" <[email protected]>', // sender address
to: '[email protected]', // list of receivers
subject: `NEW MESSAGE : ${req.body.subject}`, // Subject line
text: '', // plain text body
html: contenuFormulaireContact // html body
})
console.log('Message sent: %s', info.messageId)
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info))
res.redirect(200, '/contact')
})
Inside the nuxt.config.js i have :
/*
** Nuxt.js modules
*/
modules: [
'@nuxtjs/pwa',
'@nuxtjs/axios'
],
/*
** Axios cfg
*/
axios: {
baseURL: '/'
},
/*
** Server middleware
*/
serverMiddleware: ['~/api/index.js'],
I'm sorry if for some of you this problem may seem very obvious but I'm completely stuck.
Thank you very much in advance if you take the time to help me.