Last active
November 3, 2015 11:28
-
-
Save muit/70ec8d1d187cbd213075 to your computer and use it in GitHub Desktop.
New possible serveme syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"); |
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)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about the second? Sorry if im buggy