Comments (10)
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.
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.
NGINX supports this if you don't mind pushing it up the stack. (see: openresty and ledge)
from discussions.
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.
@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.
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.
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.
@evert Thanks.
from discussions.
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.
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)
- Improving the Koa ecosystem by moving all Koa modules to the koajs organization! HOT 62
- Immutable empty query obj HOT 15
- How to exclude api router? HOT 1
- Access to XMLHttpRequest at 'https://localhost:3443/images' from origin 'http://localhost:4200' has been blocked by CORS policy
- Destroy Mounts
- Conditional middleware
- Support for spa server?
- how to reg two part to the same file
- learning koa 2 and updating examples:https://github.com/aline2013/koa2-examples
- how to set interval within stream
- Hosting mp3 file
- Either 404 or wrong content HOT 1
- Error jump URL when ctx.path is directory
- koa-static: heavy caching issue HOT 2
- Why do i need koa-static HOT 1
- 404 not found for everything HOT 2
- koajs/conditional-get: uws.http failure
- Using both koa-compress and koa-static , extremely slow when visit static files from 127.0.0.1 or localhost (from public address is fast), why? HOT 2
- Brotli compress is slow HOT 2
- @fl0w becomes @miwnwski HOT 1
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 discussions.