Polka: no res.redirect?

Created on 18 Dec 2018  Â·  6Comments  Â·  Source: lukeed/polka

Most helpful comment

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

All 6 comments

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

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

TIL. Thanks man.

On Wed, Dec 19, 2018, 1:59 PM Olivier Refalo notifications@github.com
wrote:

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

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/lukeed/polka/issues/78#issuecomment-448479146, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AKHcj-dZqYqeqK2G2h3tSWU557eKgDRwks5u6dW7gaJpZM4ZY_6A
.

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.

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

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();
    })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ansarizafar picture ansarizafar  Â·  3Comments

lukeed picture lukeed  Â·  10Comments

itaditya picture itaditya  Â·  3Comments

motss picture motss  Â·  3Comments

motss picture motss  Â·  4Comments