Comments (5)
the matched function will receive all the parameters captured by the route, nothing more.. you can use the normalizeFn
to add more stuff to it or to change the data format
or you could simply create a helper methods to get the request/response objects or that simply does the redirect like myAwesomeRequestHelper.redirectTo('myAwesomePage')
I would use normalize if you need this data on most of the routes and the helper method if it's something specific and/or I want to keep the default crossroads behavior (pass each captured parameter as a new argument).
cheers!
from crossroads.js.
Thanks for the tip, i was looking at normalizeFn but i could not get it to work.
Could you provide an example of how i could capture the request and response objects using the normalizeFn?
Im trying to get something like:
router.addRoute('/test/{value1}/{value2}', function (request, response, value1, value2){
// Get request metadata and based on that Redirect the user to a direfent page
})
from crossroads.js.
something like this should work:
var http = require('http'),
url = require('url'),
crossroads = require('crossroads');
http.createServer(function (req, res) {
crossroads.normalizeFn = function(str, data){
return [req, res].concat(data.vals_);
};
crossroads.parse( url.parse(req.url).pathname );
}).listen(1337, "127.0.0.1");
from crossroads.js.
Thanks Miller, that worked great.
from crossroads.js.
this was a design decision to make it more flexible, the drawback is that it requires some boilerplate depending on your use case - in this case wrapping the crossroads.parse
and crossroads.normalizeFn
into the http.createServer
callback.. - the benefit is that the router can be easily adapted for different needs (since it isn't tied to any environment or specific format)
just added an example to the wiki on the normalize section: https://github.com/millermedeiros/crossroads.js/wiki/Examples
cheers
from crossroads.js.
Related Issues (20)
- routes with query params HOT 2
- How to use routing libraries like crossroads js with preload js?
- Non-encoded slashes (/) on query params choosing wrong pattern
- Why do `setting window.location.hash` and `hasher.setHash` result in different behavior of question mark `%3F`?
- URL param names are lost : route handler should get param object with names as fields
- Route Losing Reference After First Call HOT 1
- Best way to detect param changes in the same route
- Bower update failing HOT 3
- test
- Support of native URLs HOT 2
- Support of wildcard in the beginning of the URL HOT 2
- destroy function HOT 1
- Getting params as one object not on callback arguments HOT 1
- Is this project still alive? HOT 5
- Trailing slashes are appended to routes that have query params HOT 1
- Empty url in rules
- Crossroads - 404 on route HOT 1
- Nested routes
- [ SUGGESTION ] .routed.addBefore() and .routed.addAfter()
- Unmatched routes HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crossroads.js.