Skip to content

Instantly share code, notes, and snippets.

@Maxim-Kolmogorov
Last active February 26, 2021 05:26
Show Gist options
  • Save Maxim-Kolmogorov/60e7227eef7fd25704ae5e3c76fe41d9 to your computer and use it in GitHub Desktop.
Save Maxim-Kolmogorov/60e7227eef7fd25704ae5e3c76fe41d9 to your computer and use it in GitHub Desktop.
Node.js 301 redirect for Nuxt.js
[
{ "from": "/old", "to": "https://new-site.ru" },
{ "from": "/old2", "to": "/new" }
]
import serveStatic from 'serve-static'
...
serverMiddleware: [
'~/serverMiddleware/seo.js'
],
...
const redirects = require('../301.json')
export default function (req, res, next) {
const redirect = redirects.find(r => r.from === req.url)
if (redirect) {
console.log(`redirect: ${redirect.from} => ${redirect.to}`)
res.writeHead(301, { Location: redirect.to })
res.end()
} else {
next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment