-
Notifications
You must be signed in to change notification settings - Fork 156
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
how to respond if request bypassed ? #44
Comments
There are a couple of different ways to do it, but they all involve adding the listener for the var http = require('http'),
url = require('url'),
crossroads = require('crossroads');
http.createServer(function (req, res) {
crossroads.normalizeFn = function(str, vals){
// handlers will receive request as 1st param, response as 2nd
// and an object with all captured parameters as 3rd argument.
return [req, res, vals];
};
var bypassedBinding = crossroads.bypassed.add(bypassedHandler);
// set the first 2 arguments passed to the handler
bypassedBinding.params = [req, res];
crossroads.parse( url.parse(req.url).pathname );
// we remove the listener since it isn't needed anymore
// (we can only have a single bypassed dispatch per request)
// this is important otherwise it would keep adding a new listener
// for each request.
bypassedBinding.detach();
}).listen(1337, "127.0.0.1");
function bypassedHandler(req, res, requestString) {
// do your 404 magic here!
// request object and response are available!!
} I added a link to this issue on the examples page at the wiki at the part I show how to normalize the dispatched values. This is the kind of scenario where it's good to have a tool that is flexible enough so you can change the default behavior at will, that's why I use Signals in 99% of my projects. Crossroads wasn't really created thinking about node.js specifically (I use it mostly for client side), it just happen to be flexible enough that you can use it on other environments as well. I thought about creating a "node adapter" which would pass this kind of info by default but as you can see it's just a few lines of code so I'd rather let the user be free to do whatever he want and use the same source files for all environments. Do you think it would be better to add node.js specific method/wrapper (maybe a Cheers! |
Thanks for your explanations. |
I will keep this issue open, I'm considering to add an option on the Thanks for asking it, I'm not using Node.js that much (besides to write some build/helpers scripts) so I wouldn't realize it anytime soon. Planning to add this to the dev branch next week. Cheers. |
check the dev branch, just added an option to make it easier (pass an array with default arguments to |
Here is an example on how to do it after my last commit (v0.8.0-alpha): var http = require('http'),
url = require('url'),
crossroads = require('crossroads');
http.createServer(function (req, res) {
crossroads.parse(url.parse(req.url).pathname, [req, res]);
}).listen(1337, "127.0.0.1"); way cleaner... will be definitely included on the next release. |
this feature was just merged into master and pushed to npm! |
* dev: add rest segment support. closes #43. bumps version to 0.8.0. add defaultArgs to crossroads.parse to make it easier to use on node.js. closes #44 revert normalization behavior to using constants instead of methods. closes #31 change build to nodejs add methods to toggle normalize behavior. closes #31 add switched signal. see #37.
Bypassed action is called each time crossroads can't match any route. Crossroads passes requestString parameter to the action and I can't do anything except logging. Is there any ability to send 404 or close connection? I think it would be great to have response and request objects available in the "bypassed" action.
The text was updated successfully, but these errors were encountered: