Comparing version 0.2.11 to 0.2.12
@@ -14,3 +14,4 @@ // router | ||
function getRoute(urlToParse) { | ||
var pathToParse = url.parse(urlToParse).pathname, | ||
var parsed = url.parse(urlToParse), | ||
pathToParse = url.parse(urlToParse).pathname, | ||
publicControllerRegex = /^\/([A-Za-z0-9-_]+)\/?.*/, | ||
@@ -20,3 +21,4 @@ descriptorRegex = /^\/[A-Za-z-_]+[A-Za-z0-9-_]*\/[^\/]+\/?.*/, | ||
route = { | ||
pathName: pathToParse, | ||
url: parsed.href, | ||
pathname: pathToParse, | ||
controller: 'index', | ||
@@ -23,0 +25,0 @@ chain: [{ controller: 'index', action: 'index', view: 'index'}], |
@@ -31,3 +31,3 @@ // server | ||
response: response, | ||
route: router.getRoute(request.url), | ||
route: router.getRoute('http://' + request.headers.host + request.url), | ||
url: router.getUrlParams(request.url), | ||
@@ -270,7 +270,4 @@ form: {}, | ||
// If a previous event in the request context requested a redirect, do it immediately rather than firing the controller. | ||
if ( context.redirect && context.redirect.url ) { | ||
params.response.writeHead(context.redirect.statusCode || 302, { | ||
'Location': context.redirect.url | ||
}); | ||
params.response.end(server.emit('responseEnd', params, context)); | ||
if ( helpers.size(context.redirect) ) { | ||
redirect(params, context); | ||
} else if ( controller && controller[params.route.action] ) { | ||
@@ -458,9 +455,5 @@ // If the Origin header exists and it's not the host, check if it's allowed. If so, | ||
if ( requestContext.redirect && requestContext.redirect.url ) { | ||
if ( helpers.size(requestContext.redirect) && typeof requestContext.redirect.refresh === 'undefined' ) { | ||
setCookie(params, requestContext); | ||
params.response.writeHead(requestContext.redirect.statusCode || 302, { | ||
'Location': requestContext.redirect.url | ||
}); | ||
params.response.end(); | ||
server.emit('responseEnd', params, requestContext); | ||
redirect(params, requestContext); | ||
cacheController({ | ||
@@ -475,2 +468,5 @@ controller: controllerName, | ||
} else { | ||
if ( helpers.size(requestContext.redirect) ) { | ||
redirect(params, requestContext); | ||
} | ||
includeProperties = Object.getOwnPropertyNames(include); | ||
@@ -567,2 +563,26 @@ if ( includeProperties.length > 0 && params.url.type !== 'ajax' ) { | ||
function redirect(params, context) { | ||
var statusCode = context.redirect.statusCode || 302; | ||
if ( typeof context.redirect.refresh === 'number' ) { | ||
params.response.statusCode = statusCode; | ||
params.response.setHeader('Refresh', context.redirect.refresh + ';url=' + context.redirect.url); | ||
} else { | ||
if ( context.session ) { | ||
context.session.ctznReferer = params.route.url; | ||
} else { | ||
context.session = { | ||
ctznReferer: params.route.url | ||
}; | ||
} | ||
setSession(params, context); | ||
params.response.writeHead(statusCode, { | ||
'Location': context.redirect.url | ||
}); | ||
params.response.end(); | ||
} | ||
} | ||
function cacheController(options) { | ||
@@ -569,0 +589,0 @@ var cacheContext = {}, |
{ | ||
"name": "citizen", | ||
"version": "0.2.11", | ||
"version": "0.2.12", | ||
"description": "An event-driven MVC framework for Node.js web applications.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -680,13 +680,27 @@ # citizen | ||
You can pass redirect instructions to the server that will be initiated after the request is complete. Redirects using this method within the controller are not immediate, so the controller will do everything it's been asked to do before the redirect is processed. The user agent won't receive a full response, however. No view content will be rendered or sent, but cookies and session variables will be set if specified. | ||
You can pass redirect instructions to the server that will be initiated after the request is complete. Redirects using this method within the controller are not immediate, so the controller will do everything it's been asked to do before the redirect is processed. | ||
The `redirect` object takes two properties: `statusCode` and `url`. If you don't provide a status code, citizen uses 302 (temporary redirect). | ||
The `redirect` object takes three properties: `statusCode`, `url`, and `refresh`. If you don't provide a status code, citizen uses 302 (temporary redirect). The `refresh` option determines whether the redirect uses a [Location header](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30) or the non-standard [Refresh header](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Refresh). | ||
// Initiate a temporary redirect using the Location header | ||
emitter.emit('ready', { | ||
redirect: { | ||
url: 'http://cleverna.me/login' | ||
} | ||
}); | ||
// Initiate a permanent redirect using the Refresh header, delaying the redirect | ||
// by 5 seconds | ||
emitter.emit('ready', { | ||
redirect: { | ||
statusCode: 301, | ||
url: 'http://redirect.com' | ||
url: 'http://cleverna.me/new-url', | ||
refresh: 5 | ||
} | ||
}); | ||
Using the Location header breaks (in my opinion) the Referer header because the Referer ends up being not the resource that initiated the redirect, but the resource prior to the page that initiated it. To get around this problem, citizen stores a session variable called `ctznReferer` that contains the URL of the resource that initiated the redirect, which you can use to redirect users properly. For example, if an unauthenticated user attempts to access a secure page and you redirect them to a login form, the address of the secure page will be stored in `ctznReferer` so you can send them there instead of the page containing the link to the secure page. | ||
### Include Patterns | ||
@@ -693,0 +707,0 @@ |
128618
1873
1468