David.dev 🐍

about ~ apps

17. August 2020 18:48 polka

Polka JS res.redirect

One of the very useful functions in express is res.redirect so that you can easily redirect to a different page. Polka 0.5.0 doesn't support it out of the box but some digging in GitHub revealed a creative way to do it:

app.use(function(req, res, next) {
res.redirect = location => {
let str = Redirecting to ${location};
res.writeHead(302, {
Location: location,
'Content-Type': 'text/plain',
'Content-Length': str.length,
});
res.end(str);
};
next();
})

 full credits to the author of this solution in the GitHub issue  https://github.com/lukeed/polka/issues/78