Giter VIP home page Giter VIP logo

Comments (10)

fl0w avatar fl0w commented on May 13, 2024

I'm having issues relating question to a use case which in turn makes the question weird. What problem are you trying to solve?

In any case, either you just write a middleware which you use first (and thus last), or if you want to use native http server you could wrap app.middleware maybe?

from discussions.

evert avatar evert commented on May 13, 2024

There's a few different use-cases possible, but the one I'm solving right now is that I'm working on an API that uses HAL. HAL has a feature to allow embedding of other resources.

What I want to do is intercept a request and automatically do this. Basically I want to make a 'fake' GET request that goes through the entire stack, take the response and embed the result in the true parent-request.

I've ran into a few cases like this in the past. This was PHP, but it might give a bit more context. In the PHP case I implemented a CalDAV server. In some cases you'll want to for example get some information about a specific user (identified by uri). Users are also represented as HTTP resources, so to do this, it made the most sense to just fake a HTTP request and get the user information through a local GET request. That way I could keep the 'thing' that exposes user information completely separate from the middleware that needed it.

There's also cases where I might need to see what a HTTP request would yield if it were made. For instance, I have a need to do internal HEAD requests and see what Content-Types come, and which Allow headers.

After some research I guess what I think I need to do is construct a fake 'Koa Context', maybe using a package such as node-mocks-http, but I wanted to see if the Koa authors also consider this the sanest way to do this.

Thanks for looking into this!

from discussions.

jmealo avatar jmealo commented on May 13, 2024

NGINX supports this if you don't mind pushing it up the stack. (see: openresty and ledge)

from discussions.

evert avatar evert commented on May 13, 2024

Interesting, but yea I really don't want to have to do that =)

I think the node-mocks-http package will get me there though. I haven't had a chance to test this, but I'd also imagine that I'd need something similar for HTTP2 Push anyway.

from discussions.

likegun avatar likegun commented on May 13, 2024

@evert Something like this?

    var Koa = require('koa');
    var Router = require('koa-router');
    
    var app = new Koa();
    var router = new Router();
    
    async function getUser(ctx, next) {...}
    router.get('/user/:id', getUser);

    router.get('/user/:id/someinformation', async () => {
        const fakeContext = //get fake context somehow
        await getUser(fakeContext);
        const user = fakeContext.body;
        //...rest code
    })
    
    app
    .use(router.routes())
    .use(router.allowedMethods());

from discussions.

RWOverdijk avatar RWOverdijk commented on May 13, 2024

Has anyone solved this? I want to translate websocket events to requests so I can reuse the same code for both websockets and API calls, but I haven;t been able to figure out how to do this besides actually calling localhost with for example fetch.

from discussions.

evert avatar evert commented on May 13, 2024

Node-mocks-http can do this. I ended up taking a more drastic approach and write my own koa-inspired framework fixing a few other things that were hard to do

from discussions.

RWOverdijk avatar RWOverdijk commented on May 13, 2024

@evert Thanks.

from discussions.

robertkraig avatar robertkraig commented on May 13, 2024

There are usecases, but in another framework, you might check https://stackoverflow.com/questions/16520691/consuming-my-own-laravel-api, where you may have a maintenance method which does some thing, or.. you may have a cli workflow which needs to automate the creation of a few things. Instead of having to write all that same logic somewhere else considering the interconnectedness of typical codebases, you can just instantiate a new request internally and it never has to hit the network, it simply calls within the runtime of the nodejs process. I think it's mostly useful for tooling & writing utilities which do things.

from discussions.

evert avatar evert commented on May 13, 2024

My usecase was doing bulk requests and doing programmatic HTTP/2 push. This is what I came up with if anyone is interested:

https://github.com/curveball/core

from discussions.

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.