英文:
run middlewares and handler of express app with routes and get response without having to listen to a server
问题
I understand that you want to translate the provided text into Chinese. Here's the translation:
我是否可以运行 Express 应用程序的中间件和处理程序与路由,并在不必侦听服务器的情况下获取响应?
我有一个如下的 Express 应用程序:
var express = require("express")();
express.get("/test", (req, res) => {
console.log("TESTING CLUSTER PROCESS ", process.pid);
res.status(200).send(`{ 'hello': 'server', 'pid': ${process.pid} }`)
})
express.all("/", (req, res) => {
console.log("TESTING CLUSTER PROCESS ", process.pid);
res.status(200).send(`{ 'hello': 'server', 'pid': ${process.pid} }`)
})
module.exports = express;
类似于这样:
let req = new require("https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/request.js")
req.path = "/";
req.header = {};
req.body = '{"test": "tester"}';
let res = new require("https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/response.js")
let someresponse = router.handle(req, res);
console(someresponse);
我希望使用 app.handle(req, res, next)
或 router.handle(req, res, next)
并在不必侦听服务器的情况下使用 app 路由
。
我是否可以运行 req-res 函数而无需使用创建 http/s 服务器?我将从 fetch 请求创建请求(希望模拟此请求对象而不是 fetch api https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/request.js#L31 和从此响应对象获取响应 https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/response.js#L43)以运行 router.handle(req, res, next)
并获取响应,而无需侦听 http 服务器。
是否可以在上述这种 app 实例中逻辑上实现这一点?
请求
> 请求对象 > app(带有路由,无需侦听)
> 处理请求
以获取通过路由处理程序的响应 > 响应
- 这是我试图实现的目标。
假设目标是通过路由、中间件和处理程序运行请求对象,获取响应而无需侦听 Express 服务器。
英文:
This is in continuation of the question here. https://stackoverflow.com/questions/76193074/middleware-and-stack-functions-invoker-for-expressjs-and-the-response-object-cap/76221423
Can I run middlewares and handler of express app with routes and get response without having to listen to a server
I have an express app like below:
var express = require("express")();
express.get("/test", (req, res) => {
console.log("TESTING CLUSTER PROCESS ", process.pid);
res.status(200).send(`{ 'hello': 'server', 'pid': ${process.pid} }`)
})
express.all("/", (req, res) => {
console.log("TESTING CLUSTER PROCESS ", process.pid);
res.status(200).send(`{ 'hello': 'server', 'pid': ${process.pid} }`)
})
module.exports = express;
something like this:
let req = new require("https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/request.js")
req.path = "/";
req.header = {};
req.body = '{"test": "tester"}';
let res = new require("https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/response.js")
let someresponse = router.handle(req, res);
console(someresponse);
I am wishing to use the app.handle(req, res, next)
or the router.handle(req, res, next)
and use
the app routes wihtout having to listen to a server
.
Is it possible for me to run the req-res function without using the creation of http/s server using this? I will create a request from fetch request (wish to mock this request object instead of fetch api https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/request.js#L31 and response from this response object https://github.com/expressjs/express/blob/0debedf4f31bb20203da0534719b9b10d6ac9a29/lib/response.js#L43 to run the router.handle(req, res, next)
and get the response without having to listen to a http sever.
Is it possible - logically with such app instances above?
request
> request object > app (with routes, without listening)
> process request
to get response through route handler
> response
- is what I am trying to achieve
Lets say the goal is run the request object through the routes, middlewares, handlers, get the response without having to listen to the express server.
答案1
得分: 1
如果您想自己调用 router.handle(req, res, next)
,那么您必须创建具有 Express(以及您可能使用的任何中间件库)所期望和使用的所有属性和方法的自己的 req
和 res
对象。从 Express 的角度来看,您手动创建的 req
和 res
对象必须看起来与通常由 http 服务器对象创建的对象(以及 Express 将添加或覆盖的方法)无法区分。
这意味着必须有传入流、传出流以及req
对象预解析的状态(可能已包含已解析的标头,但没有主体数据),以及所有适当的属性和方法。然后,任何主体数据(来自 POST 或 PUT)必须在 req
流中等待被中间件或路由处理程序读取。
这一切都是可以实现的,只是需要大量的繁重工作来正确完成,然后进行彻底测试以查看是否确实遗漏了任何内容。然后,当某些事情无法正常工作时,您可能会疑惑您的“伪造”服务器实现是否有问题,或者实际上是您的路由处理程序中的错误。
当然,您还必须创建一个 next()
回调函数,以在有参数和无参数的情况下调用时具有适当的行为。
就个人而言,我不太明白为什么您不只需创建一个本地的 http 服务器(通过一个简单的函数调用),让 http
服务器对象为您完成所有这些工作。您尚未提供任何动机来解释为什么要做所有这些额外的工作,因此我们无法评论解决您尝试解决的任何问题的其他(可能更好的)方式。
英文:
If you want to call router.handle(req, res, next)
yourself, then you have to create your own req
and res
objects with all the properties and methods that Express (and any middleware libraries you might use) expects and uses. From Express' point of view, your manually created req
and res
objects must appear to be indistinguishable from the ones that the http server object would normally create (and with the methods that Express would add or override).
That means there needs to be incoming stream, an outgoing stream and whatever pre-parsed state the req
object is expected to be in (presumably containing already parsed headers, but no body data) and all the appropriate properties and methods. Then, any body data (from a POST or PUT) must be in the req
stream waiting to be read by your middleware or route handlers.
It's all doable, just a lot of grunt work to get right and then testing it thoroughly to see if you actually missed anything. Then, when something doesn't work properly you'll be wondering if your "fake" server implementation is the problem or if its actually a bug in your route handler.
And, of course, you have to create a next()
callback function that has the appropriate behavior when called with and without a parameter.
Personally, I don't quite understand why you don't just create a local http server (with one simple function call) and let the http
server object do all this work for you. You have not offered any motivation for why you want to do all this extra work so we can comment on other (perhaps better) ways to solve whatever problem you're trying to solve.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论