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

add redirect support to res #803

Merged
merged 2 commits into from
May 21, 2015
Merged

add redirect support to res #803

merged 2 commits into from
May 21, 2015

Conversation

DonutEspresso
Copy link
Member

Added a res.redirect method, which is primarily a sugar method, but makes redirects a lot easier to manage within Restify. I used the node core url module to avoid having to reinvent the wheel for URL parsing, but am open to suggestions.

Edit: Another nice thing about having redirect is to avoid the need for users to call next(false) after doing a manual redirect via res.send(302). Calling next with false is required to stop the handler execution chain today. Given that, we will throw if redirect is invoked without next.

// redirect to another domain 
res.redirect('http://www.foo.com', next);
res.redirect(301, 'http://www.foo.com', next);
// redirect to another url on same domain
res.redirect('/foo', next);
// reload the page on https using the options object.
// reload is implied due to missing pathname and hostname.
res.redirect({
    secure: true
}, next);
// redirect while appending to existing query string params
res.redirect({
    pathname: '/foo',
    query: {
        foo: 'bar'
    }
}, next); 
// redirect while stomping on existing query string params
res.redirect({
    pathname: '/foo',
    overrideQuery: true,
    query: {
        foo: 'bar'
    }
}, next); 
// redirect to another host/path using options object
// => https://www.foo.com/bar?a=1
res.redirect({
    hostname: 'www.foo.com',
    pathname: '/bar',  
    secure: true,
    query: {
        a: 1
    }
}, next);

Don't feel particularly strong about all the extra sugar properties - but I do think reload to https is an especially common use case.

fix failing lint and style checks

remove URIjs in favor of standard node core url module
}

// now we're done constructing url, send the res
this.send(statusCode, null, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use this. declare var self at the top of the function and use self.send()

yunong added a commit that referenced this pull request May 21, 2015
@yunong yunong merged commit 5901083 into restify:master May 21, 2015
@BreaGaudioso
Copy link

@DonutEspresso Is the body of the response on this supposed to return null?

@DonutEspresso
Copy link
Member Author

There's currently no facility to deliver a payload using this redirect API. Feel free to file an issue if this is something you'd like to see! In the meantime, I'd recommend doing a plain old res.send with the appropriate header and status codes.

@BreaGaudioso
Copy link

Thanks for the quick response!

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

Successfully merging this pull request may close these issues.

3 participants