Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server middleware is not included in .nuxt production folder #9158

Closed
jozef-rzadkosz opened this issue Apr 15, 2021 · 11 comments
Closed

Server middleware is not included in .nuxt production folder #9158

jozef-rzadkosz opened this issue Apr 15, 2021 · 11 comments

Comments

@jozef-rzadkosz
Copy link

Versions

  • nuxt: 2.14.12
  • node: 14.15.1

Reproduction

Nuxt build server-middleware is not included in .nuxt build folder. I have one server-middleware with one express endpoint like: /contact/send and inside of it I am just sending simply post request and send an email from it by external email provider (SendGrid). Everything is working good but on dev mode, on production I am getting 404 and I cannot target my /contact/send endpoint anymore.

Here's a question in stackoverflow
https://stackoverflow.com/questions/67111986/nuxt-server-middleware-not-working-on-production/67113206?noredirect=1#comment118629365_67113206

Steps to reproduce

  1. create simply server-middleware
  2. Build app and host it anywhere
  3. Try to request it with httpie or postman etc.

If you cannot host this write this:

app.get('/test', (req,res) => {
res.send('test');
})

After that try to find this in your production folder "test" and you will get no matching

What is Expected?

When I write some endpoint to target I need to have it also in production mode after build.

What is actually happening?

Server middleware function are not included in .nuxt folder and we cannot target them in production mode

Copy link
Member

@jozef-rzadkosz 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.)

@bhumit070
Copy link

bhumit070 commented Apr 23, 2021

but when i run npm run build ; npm run start it shows in console that db connected , which I have console logged from server's js file so why is that
and this is what i got in console when nuxt app runs

│ Nuxt @ v2.14.12 │
│ │
│ ▸ Environment: production │
│ ▸ Rendering: client-side │
│ ▸ Target: server │
│ │
│ Memory usage: 57.2 MB (RSS: 128 MB) │
│ │
│ Listening: http://localhost:3000/
│ │

and i can hit apis too but when i run same commands on server it does not work why ?

Copy link
Member

@bhumit070 I'm not sure what you're asking.

@bhumit070
Copy link

bhumit070 commented Apr 23, 2021

I am asking the server middleware works on production build but at deploy time if i run the same command npm run build and npm run start

i run command npm run build and npm run start to start the production build

why my server middleware is not working

Copy link
Member

danielroe commented Apr 23, 2021

@bhumit070 My best guess is that it is because your serverMiddleware are in your project on your local machine but when you deploy you are only copying across .nuxt and not your serverMiddleware.

Make sure to follow my suggestion in #9158 (comment) and you should be fine 👍 .

@bhumit070
Copy link

bhumit070 commented Apr 23, 2021

how could that be possible what i do is I push to github and then it is connected to azure account which deploys automatically

Copy link
Member

@bhumit070 It sounds like there is an issue with the deploy setup, but without more details I couldn't say what the issue is. I would suggest you join https://discord.nuxtjs.org and ask for support there, where it's easier to provide back-and-forth questions and answers.

@SergiuPlesco
Copy link

@jozef-rzadkosz 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.)

Hi, I am new to nuxt.
Can you point to a source how to do copying of the serverMiddleware into .nuxt on production. I am confused.

Thanks

@danielroe
Copy link
Member

@SergiuPlesco That is only true for Nuxt 2. You can ignore this issue.

@SergiuPlesco
Copy link

@SergiuPlesco That is only true for Nuxt 2. You can ignore this issue.

Thanks for your reply. The project i am working on is Next 2.
It simple one. The only functionality it has is sending emails with mailjet when a form is submited. And it uses a serverMiddleware.
Will upgrading to next 3 solve the issue or copying will l?

Thanks

Copy link
Member

I would advise upgrading to Nuxt 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants