Skip to content

Commit

Permalink
reply.redirect(). Closes #1688
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Jun 5, 2014
1 parent 830ddf6 commit 842ce83
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
29 changes: 24 additions & 5 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [`reply.view(template, [context, [options]])`](#replyviewtemplate-context-options)
- [`reply.close([options])`](#replycloseoptions)
- [`reply.proxy(options)`](#replyproxyoptions)
- [`reply.redirect(location)`](#replyredirectlocation)
- [Response object](#response-object)
- [Response events](#response-events)
- [`Hapi.error`](#hapierror)
Expand Down Expand Up @@ -1632,7 +1633,7 @@ var pre = function (request, reply) {
### `reply([result])`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Concludes the handler activity by returning control over to the router where:

Expand Down Expand Up @@ -1673,7 +1674,7 @@ Note that if `result` is a `Stream` with a `statusCode` property, that status co
### `reply.file(path, [options])`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime type based on filename extension.:

Expand All @@ -1700,7 +1701,7 @@ var handler = function (request, reply) {
### `reply.view(template, [context, [options]])`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Concludes the handler activity by returning control over to the router with a templatized view response where:

Expand Down Expand Up @@ -1753,7 +1754,7 @@ server.route({ method: 'GET', path: '/', handler: handler });
### `reply.close([options])`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Concludes the handler activity by returning control over to the router and informing the router that a response has already been sent back
directly via `request.raw.res` and that no further response action is needed. Supports the following optional options:
Expand All @@ -1766,7 +1767,7 @@ The [response flow control rules](#flow-control) **do not** apply.
### `reply.proxy(options)`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, or `reply.proxy()` is called._
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Proxies the request to an upstream endpoint where:
- `options` - an object including the same keys and restrictions defined by the [route `proxy` handler options](#route.config.proxy).
Expand All @@ -1782,6 +1783,24 @@ var handler = function (request, reply) {
};
```

### `reply.redirect(location)`

_Available only within the handler method and only before one of `reply()`, `reply.file()`, `reply.view()`,
`reply.close()`, `reply.proxy()`, or `reply.redirect()` is called._

Redirects the client to the specified location. Same as calling `reply().redirect(location)`.

Returns a response object.

The [response flow control rules](#flow-control) apply.

```javascript
var handler = function (request, reply) {

reply.redirect('http://example.com');
};
```

## Response object

Every response includes the following properties:
Expand Down
2 changes: 1 addition & 1 deletion lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ exports.handler = function (route, options) {
!request.server.settings.router.stripTrailingSlash &&
!hasTrailingSlash) {

return reply().redirect(resource + '/');
return reply.redirect(resource + '/');
}

if (!index) {
Expand Down
5 changes: 5 additions & 0 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ exports.replyInterface = function (request, finalize, base) {
request._clearState(name);
};

reply.redirect = function (location) {

return internals.wrap('', request, finalize).redirect(location);
}

return reply;
};

Expand Down
6 changes: 3 additions & 3 deletions test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ describe('Proxy', function () {

var redirectHandler = function (request, reply) {

reply().redirect('/redirect?x=1');
reply.redirect('/redirect?x=1');
};

var upstream = new Hapi.Server(0);
Expand Down Expand Up @@ -833,7 +833,7 @@ describe('Proxy', function () {

var redirectHandler = function (request, reply) {

reply().redirect('/profile');
reply.redirect('/profile');
};

var profile = function (request, reply) {
Expand Down Expand Up @@ -894,7 +894,7 @@ describe('Proxy', function () {
it('redirects to a post endpoint with stream', function (done) {

var upstream = new Hapi.Server(0);
upstream.route({ method: 'POST', path: '/post1', handler: function (request, reply) { reply().redirect('/post2').rewritable(false); } });
upstream.route({ method: 'POST', path: '/post1', handler: function (request, reply) { reply.redirect('/post2').rewritable(false); } });
upstream.route({ method: 'POST', path: '/post2', handler: function (request, reply) { reply(request.payload); } });
upstream.start(function () {

Expand Down

0 comments on commit 842ce83

Please sign in to comment.