Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no res.redirect? #78

Closed
orefalo opened this issue Dec 18, 2018 · 6 comments
Closed

no res.redirect? #78

orefalo opened this issue Dec 18, 2018 · 6 comments

Comments

@orefalo
Copy link

orefalo commented Dec 18, 2018

No description provided.

@lukeed
Copy link
Owner

lukeed commented Dec 18, 2018

Not yet! On the agenda for 1.0 – see #5 and #14 🙌

@lukeed lukeed closed this as completed Dec 18, 2018
@orefalo
Copy link
Author

orefalo commented Dec 19, 2018

Think I figured it out

`
import * as encodeUrl from 'encodeurl';
...

res.writeHead(301, { Location: encodeUrl(myurl) });
res.end();
`

Thanks for the help, think I would have never find it out alone

@motss
Copy link

motss commented Dec 19, 2018 via email

@lukeed
Copy link
Owner

lukeed commented Dec 19, 2018

Oh, sorry guys. Didn't know there was a question for how to do a redirect. Thought this was just a tracker for res.redirect to exist.

let url = 'https://.../foo/bar';
let str = `Redirecting to ${url}`;

res.writeHead(302, {
	Location: url,
	'Content-Type': 'text/plain',
	'Content-Length': str.length
});

res.end(str);

This is pulled from one of my projects. It displays text in the browser for a moment – which isn't necessary but I chose to have it here.

@viktor89
Copy link

for compatibility with with other express middlewares like passport, a res.redirect function might also be a good idea

@viktor89
Copy link

viktor89 commented Mar 18, 2020

should anyone stumble upon this, I solved my issue by added the method to the res object in a middleware like this:

polka()
    .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();
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants