Giter Club home page Giter Club logo

Comments (5)

millermedeiros avatar millermedeiros commented on July 20, 2024

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.

rfrancisco avatar rfrancisco commented on July 20, 2024

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.

millermedeiros avatar millermedeiros commented on July 20, 2024

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.

rfrancisco avatar rfrancisco commented on July 20, 2024

Thanks Miller, that worked great.

from crossroads.js.

millermedeiros avatar millermedeiros commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.