Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created July 15, 2012 16:29
Show Gist options
  • Save juandopazo/3117644 to your computer and use it in GitHub Desktop.
Save juandopazo/3117644 to your computer and use it in GitHub Desktop.
Express vs YAF

I think the goal should be to replace Express with Y.App. Express does:

  • Routing
  • Redirection
  • Environment configuration
  • View templating and rendering
  • Everything Connect does

Routing and redirection are very much in the scope of Y.Router/Y.App. Environment configuration is in the scope of YUI. View templating and rendering are also covered. Rendering of static files, middleware, logging, caching, cookies, compression, etc are all covered by Connect. So... Shouldn't the goal be to make Y.App work nicely with Connect?

// this is completely without any real thought behind it:
var app = new Y.App();
app.use(connect.static());

//Should I post this in the forums?

@jshirley
Copy link

+1

@hojberg
Copy link

hojberg commented Jul 15, 2012

👍

@juandopazo
Copy link
Author

I'm starting to experiment with a server side Router using connectApp.use(Y.Router.middleware); with this basic idea:

middleware: function (req, res, next) {
    if (!Y.Router.dispatch(req, res)) {
        next();
    }
},

/**
Dispatches to the first route handler that matches the specified `path` for
all active router instances.

This provides a mechanism to cause all active router instances to dispatch
to their route handlers.
This method should be called from each request made to the server

@method dispatch
@static
@param [ServerRquest] request
@param [ServerResponse] response
@returns [Boolean] true if any route matched
@since 3.6.0
**/
dispatch: function (req, res) {
    var i, len, router,
        routed = false,
        url;

    for (i = 0, len = instances.length; i < len; i += 1) {
        router = instances[i];

        if (router) {
            router._req = req;
            router._res = res;
            url = router._getUrl();
            if (!routed && router.hasRoute(url)) {
                routed = true;
            }
            router._dispatch(router._getPath(), url);
        }
    }
    return routed;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment