1

I think i'm misunderstanding nuxt server middleware. I have an api directory with a single file that spins up express and writes records to a mysql database. Everything works as expected running in dev, but after nuxt generate, that endpoint always returns a 404.

Can you use nuxt generate with an internal API that runs on the server? I'd be using axios to hit this endpoint for what it's worth but I doubt that makes a difference.

from Nuxt config:

{
  target: static, 
  serverMiddleware: [{ path: '/api', handler: '~/api/index.js' }]
}

1 Answer 1

0

Server middleware basically allows you to run your own code on the server nuxt spins up for you in target: 'server' mode. Therefore your approach won't work in static mode, as there is no server running your middleware. You are only serving static files. In dev mode you are running a server locally, which is why your middleware works fine there.

For it to work in production you have two options:

  • Run nuxt in target: 'server' mode, instead of generating static files
  • Spin up your express server separately, instead of using server middleware in nuxt

I hope you understand how server middleware works now and you can make the right decision for your project!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Florian, got that part. But, now how do I run the site without a dist directory? It says listening on localhost but I want it to run from a local vhost like mynuxtsite.com?
@MattKaye I suppose you mean running in target: 'server'; mode by saying 'without a dist directory'. I'm not experienced with setting up a vhost, but you can take a look at the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.