BibekStha
BibekStha

Reputation: 180

Is there a way to redirect a NuxtJs application using Express server?

I have a NuxtJs application initialized with Express server using npx create-nuxt-app <project-name>. It is set for server-side rendering.

Express has access to NuxtJs middleware like so. ( Which comes by default when Nuxt app is created )

app.use(nuxt.render)

Now I have created a different route file in server side that handles API routes. This route works as I can access data using Axios. I have added this route right before the above code, like this. ( API routes don't work if it is added after )

app.use('/api', apiRoutes)
app.use(nuxt.render)

There is a route where, after some operation, I need to redirect the application to another page. I tried using res.redirect('/some-route'), which is an Express way for redirection but that didn't work.

Am I missing something here? Is there some other way we do redirection from server side in Nuxt application that I'm totally unaware of?

Upvotes: 4

Views: 1433

Answers (1)

BibekStha
BibekStha

Reputation: 180

Finally found the way to redirect from the back-end.

res.writeHead(301, { Location: url })
res.end()

Here is the link to Nuxt GitHub issue comment for detailed example using server middleware.

Alternatively, you could respond with desired value to the font-end and based on the value received, you can re-direct from the front-end.

Upvotes: 2

Related Questions