Git Product home page Git Product logo

Comments (12)

Dashron avatar Dashron commented on July 26, 2024

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.

Dashron avatar Dashron commented on July 26, 2024

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.

dancespiele avatar dancespiele commented on July 26, 2024
  • @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.

Dashron avatar Dashron commented on July 26, 2024

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.

dancespiele avatar dancespiele commented on July 26, 2024

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.

Dashron avatar Dashron commented on July 26, 2024

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.

  1. From within middleware added before the route action, put your "after-route" logic in the "then" handler of the promise returned from next();
  2. Add middleware after the route action and make sure your route calls next();

Let me know if you want further examples

from roads.

dancespiele avatar dancespiele commented on July 26, 2024

It would be cool an example about this case that I mentioned before please

from roads.

Dashron avatar Dashron commented on July 26, 2024

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.

dancespiele avatar dancespiele commented on July 26, 2024

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.

Dashron avatar Dashron commented on July 26, 2024

I have only two small comments on your example.

  1. 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 a next that just immediately returns.
  2. 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 call next() 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);
        }
    });
});
  1. 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.

dancespiele avatar dancespiele commented on July 26, 2024

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.

dancespiele avatar dancespiele commented on July 26, 2024

I consider that the parameters is resolved because of this I closed this issue

from roads.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.