Skip to content

Instantly share code, notes, and snippets.

@Maxim-Kolmogorov

Maxim-Kolmogorov/301.json

Last active Feb 26, 2021
Embed
What would you like to do?
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