Answer the question
In order to leave comments, you need to log in
How to make redirects in nodejs?
Hello.
I have a vue + ssr app
Actually ssr itself provides a node server.
On it I try to make the redirects I need.
The first one is a 301 redirect from a url with a slash to a url without a slash.
Tried to do so
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
const stripTrailingSlash = url => url.replace(/\/$/, "");
const urlArray = fullUrl.split('');
res.setHeader("Content-Type", "text/html");
res.setHeader("Server", serverInfo);
if (urlArray[urlArray.length - 1] === '/') {
res.writeHead(301, {'Location': stripTrailingSlash(fullUrl)});
res.end();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question