Reputation: 5508
I have builded an app using Nuxt and I have created simply server middleware for handling email sending. Everything is working on dev but on production I have 404 error from that endpoint. Does anybody know how to include server-middleware into build files or any other way?
server-middleware:
const bodyParser = require('body-parser')
const app = require('express')()
app.use(bodyParser.json())
app.post('/', (req, res) => {
// Some code here
})
module.exports = app
Nuxt.config.js
serverMiddleware: [
{ path: '/contact/send', handler: '~/server-middleware/email.js' }
],
Upvotes: 2
Views: 6717
Reputation: 46632
Here is an in-depth answer regarding the whole setup of a serverMiddleware.
This solutions seems to work: https://github.com/nuxt/nuxt.js/issues/1486#issuecomment-325181524
// nuxt.config.js
serverMiddleware: [
'~/api/index.js',
]
// api/index.js
const app = require('express')()
module.exports = { path: '/api', handler: app }
app.get('/say/:word', (req, res) => {
res.json(req.params)
})
Upvotes: 0
Reputation: 5508
I found an answer on github issue nuxt repo:
This is correct - server middleware aren't compiled or part of your webpack build and so you should make sure to copy them separately into production. (As well as making sure that any dependencies they have are installed in production.)
(Note that this will change with Nuxt 3 - you'll be able to have a single built server that includes your server middleware.)
Issue here: https://github.com/nuxt/nuxt.js/issues/9158#issuecomment-820676790
Upvotes: 2