-
-
Save muit/70ec8d1d187cbd213075 to your computer and use it in GitHub Desktop.
//ServeMe - github.com/muit/serveMe | |
var server = ServeMe(); | |
server.get("/user").do(authenticated).do(function(req, res) { | |
return { | |
json: { | |
message: "Hey!" | |
} | |
}; | |
}); | |
server.start(3000); | |
function authenticated(req, res){ | |
if(req.isAuthenticated) { | |
res.next(); | |
} else { | |
return {status: 401}; | |
} | |
} |
//ServeMe - github.com/muit/serveMe | |
var server = ServeMe(); | |
/** | |
* Send an optional object with parameters. | |
* For example "require" to continue or not depending on the bool return of the module. | |
* (modules are similar to middlewares but not the same, becouse they indicate an action or a behaviour before the actual route) | |
**/ | |
server.get("/user", function(req, res) { | |
return { | |
json: { | |
message: "Hey!" | |
} | |
}; | |
}).require(authenticated); | |
function authenticated(req, res){ | |
return req.isAuthenticated; | |
} | |
server.start(3000); |
//The Callback is opcional. The default behaviour will be serving a webpage. | |
//First option | |
server.get("/user", { | |
do: doSomething, //(optional) | |
require: auth, | |
fail: failCallback | |
}); | |
server.get("/user", { | |
do: doSomething, //(optional) | |
require: auth, | |
fail: "redirectTo:/home" | |
}); | |
//Second option | |
server.get("/user", doSomething).require(auth, failCallback); | |
server.get("/user", doSomething).require(auth, "redirectTo:/home"); |
mm why should it need a decorator instead of the direct function?
about the promises im waiting for the implemented es6 promises, but even with that im porting serveme to es6... and.. that "then" is not a promise, maybe i need to change "then" to another thing like "do" becouse its just a middleware
I would not recommend using promises for this. Would make the flow difficult to reason about and compose.
mmm ok
thanks :P
What about the second? Sorry if im buggy
Not easy to customize the behaviour in a require
Ok Thanks for the opinions, will implement something, maybe cool
Oh I had an idea right now. What if the routes allow you to modify the behaviour of the application like, denying access to some static direction with a require. Example better:
server.get("/home").require(authenticated);
//or
server.get("/home").require(authenticated, "/redirect");
The route is no longer just a function, it just modifies the default behaviour of that route (that is serving static files)
Overall I like the syntax, but I have some subjective feedback:
Great work!