orangemug / isorouter

A simple router
1 stars 1 forks source link

Distinguish between middleware and handlers which respond #16

Open oliverbrooks opened 8 years ago

oliverbrooks commented 8 years ago

Use case:

I have an app in which some routes are only relevant on the server. When I do a GET or POST to these routes I want the request to perform a full HTTP request rather than attempt an XHR and 404.

This is currently possible by using a fully qualified URL which isorouter treats as a full page refresh but it requires knowledge that route x needs this special treatment.

Would propose that the route matching return false if there is no handler which matches the given pathname. For example.

isomorphic_routes.js

isorouter.use(middleware1);
isorouter.use(middleware2);

isorouter.get("/fish", function () {});

server_routes.js

router.get("/face", function () {});

If you hit this app with GET /fish it will run middleware 1 and 2 and also the fish handler and do so on both client and server side.

If you hit this app with GET /face' with HTTP it will handle the request but fail from a loaded client app. If you tap a link to/face` it will run through the middleware and fail silently. Sad face.

Would be nice if isorouter could detect that there is no handler for a pathname matching the link <a href="/face"> and perform a HTTP request.

In the current code this would involve returning false to router.go() called from lib/dom_event_listener.js so the event isn't cancelled. This could be done by modifying the go method in browser.js to only return true if there was one or more route match (ignore middleware).

orangemug commented 8 years ago

I guess the other method would be to guard around the server side routes

app.get("/serverOnly", passThroughServer, function() {})

passThroughServer could then just redirect the page

Then the server / client code can live in the the same place in the codebase.

Not sure if thats a good idea but thought I'd put it out there