Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect all routes to https in nuxt project hosted in heroku

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}`);
   }
  }
}
like image 942
yoyojs Avatar asked Jun 17 '19 10:06

yoyojs


1 Answers

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"],
like image 179
yoyojs Avatar answered Oct 19 '22 09:10

yoyojs