I'm trying to create a middleware to redirect all my routes to https, I think I need a middleware so I've created a redirect.js
file in the middleware folder of Nuxt and then I've added this to nuxt.config.js
:
router: {
middleware: ["redirect"]
},
And here is my redirect.js
file that gives me a server error:
export default function({ app }) {
if (process.env.NODE_ENV === "production") {
if (app.context.req.header("x-forwarded-proto") !== "https") {
app.context.res.redirect(`https://${app.context.req.header("host")}${app.context.req.url}`);
}
}
}
I found an easier way i've added the package redirect-ssl
npm i redirect-ssl
and then i've added this line in my nuxt.config.js :
serverMiddleware: ["redirect-ssl"],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With