Skip to content

Instantly share code, notes, and snippets.

@muit
Last active November 3, 2015 11:28
Show Gist options
  • Save muit/70ec8d1d187cbd213075 to your computer and use it in GitHub Desktop.
Save muit/70ec8d1d187cbd213075 to your computer and use it in GitHub Desktop.
New possible serveme syntax
//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");
@gootyfer
Copy link

Overall I like the syntax, but I have some subjective feedback:

  • I'd used decorator-like style for authentication
  • not sure if it's better to encapsulate the promises syntax in your library and leave the API more simple
    Great work!

@muit
Copy link
Author

muit commented Sep 30, 2015

mm why should it need a decorator instead of the direct function?

@muit
Copy link
Author

muit commented Sep 30, 2015

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

@Serabe
Copy link

Serabe commented Sep 30, 2015

I would not recommend using promises for this. Would make the flow difficult to reason about and compose.

@muit
Copy link
Author

muit commented Sep 30, 2015

mmm ok
thanks :P

@muit
Copy link
Author

muit commented Sep 30, 2015

What about the second? Sorry if im buggy

@Serabe
Copy link

Serabe commented Sep 30, 2015

Not easy to customize the behaviour in a require

@muit
Copy link
Author

muit commented Sep 30, 2015

Ok Thanks for the opinions, will implement something, maybe cool

@muit
Copy link
Author

muit commented Sep 30, 2015

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)

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