Comments (12)
Colon isn't an accepted URI templates prefix in this router. Use # for numbers and $ for anything else.
Your example should look like /user/#id.
I'm a little behind on the docs for this, I'll give it a pass tomorrow.
from roads.
I just gave a first pass to the SimpleRouter docs: https://github.com/Dashron/roads#simplerouterroad-road. Let me know if you have any questions.
Also, I wanted to give you heads up that I'm moving the body parsing into middleware. I'll make sure to document the change.
from roads.
- @Dashron I like the idea to put the prefix # and $ because everything that you have to expecify the type is good. I think in the docs is mixing ‘SimpleRouter.midleware(args...)‘
from roads.
At the moment I'm keeping that private, I should probably rename it to _middleware
. There's some weird stuff with the request context that makes it complex to open up publicly.
You should use applyMiddleware
.
from roads.
Ok, but I have a question. If I want to create a middleware before or after of execute a route, how would I do it? Image that I want to check something before of execute this route, a validation data for example.
from roads.
There are some examples in the use
documentation here: https://github.com/Dashron/roads#use-callback. The tl;dr is that you should add middleware before you mount the router for any before-route action, and you have two options for post-route actions.
- From within middleware added before the route action, put your "after-route" logic in the "then" handler of the promise returned from next();
- Add middleware after the route action and make sure your route calls next();
Let me know if you want further examples
from roads.
It would be cool an example about this case that I mentioned before please
from roads.
sure. I just added a new section to https://github.com/Dashron/roads/blob/master/README.md#use-callback called "How do I control the order my logic executes?". Let me know if that clears it up
from roads.
Yes now I understand completly. For example to create a before and after middleware I can do this.
app.use(function (method, path, body, headers, next) {
console.log('A ' + method + ' request was made to ' + JSON.stringify(path));
if(next) return next();
});
router.addRoute("GET", "/user/#id", (url: any, body: any, headers: Headers, next: Function) => {
console.log(`These are the parameter user ${JSON.stringify(url.args)}`);
return next();
});
router.addRoute("GET", "/person", (url: any, body: any, headers: Headers, next: Function) => {
console.log("This is a example");
return next();
});
router.applyMiddleware(app);
app.use((method, path: any, body, headers, next) => {
if(path.path === '/user/5') {
console.log("10");
return new Response({id: "10"}, 200);
}
if(next) return next();
});
from roads.
I have only two small comments on your example.
- You will always have
next
, so you shouldn't need to wrap that in an if statement. Even if there is no additional middleware you will get anext
that just immediately returns. - The final function (with the path check) might not always execute. If your logic in the router doesn't call
next()
, it won't check for/user/5
. To ensure you don't have to remember to callnext()
in every single router function I recommend you include that after logic in your "this is an example" section like this.
router.addRoute("GET", "/person", (url: any, body: any, headers: Headers, next: Function) => {
console.log("This is a example");
return next()
.then(function (response) {
if(path.path === '/user/5') {
console.log("10");
return new Response({id: "10"}, 200);
}
});
});
- If you provide arrow functions to the
use
function, they won't have the proper request context. This is a limitation of arrow functions and unfortunately something I'm not able to work around.
from roads.
About the if was some problem with the typescript that I put it as optional argument and typescript force you to put if
, anyway I changed it and now It is required to put it as argument then is no necessary if
.
About the second point I have to do like this how I'm doing, I know that I have to call the next
function If I want execute the next middleware otherwise I cannot create decorations https://github.com/spieljs/spiel-server/blob/master/test/server.ts, https://github.com/spieljs/spiel-server/blob/master/src/server/set-router.ts
from roads.
I consider that the parameters is resolved because of this I closed this issue
from roads.
Related Issues (20)
- Extract field expansion and filtering into a separate module HOT 1
- Remove extras HOT 1
- Remove generator dependency HOT 1
- Client side support HOT 3
- Response wrapping should happen before the request handler is called HOT 2
- OPTIONS requests are broken HOT 1
- Support more generic routing options HOT 2
- Support public and private roads HOT 2
- Representation is not used in collection controller of the example
- Build real homepage
- Add max age header so CORS can be cached HOT 1
- types file for roads HOT 18
- The docs is mixing for cookies HOT 5
- Issue with the response in the version 5.0.0-alpha.14 HOT 20
- cors middleware does not work HOT 1
- Pass data between middleware HOT 1
- Warning node message without use http2 HOT 2
- Parse body issue HOT 1
- Go back and tag releases for old versions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roads.