简体   繁体   English

如何手动创建 http.IncomingMessage stream?

[英]How can I manually create http.IncomingMessage stream?

I want to manually create http.IncomingMessage stream with some header & body.我想手动创建 http.IncomingMessage stream 和一些 header 和正文。 As it is a readable stream, I can't write/pipe data into it.由于它是可读的 stream,因此我无法将数据写入/管道。 Is it safe to extend it?延长它是否安全?

Also, I am not sure how to store headers when it's a stream which doesn't operate on ObjectMode.另外,当它是不在 ObjectMode 上运行的 stream 时,我不确定如何存储标头。

EDIT:编辑:

According to Node.js documentation , extending http.IncomingMessage is possible.根据Node.js 文档,扩展 http.IncomingMessage 是可能的。

IncomingMessage <http.IncomingMessage> Specifies the IncomingMessage class to be used. IncomingMessage <http.IncomingMessage> 指定要使用的 IncomingMessage class。 Useful for extending the original IncomingMessage.对于扩展原始 IncomingMessage 很有用。 Default: IncomingMessage.默认值:传入消息。

How can I extend it?我该如何扩展它? Its constructor asks for Socket object.它的构造函数请求Socket object。

I mocked it like this in my tests:我在测试中这样嘲笑它:

import { IncomingMessage } from "http";
import { Socket } from "net";

const msg = new IncomingMessage(new Socket());
msg.method = 'POST';
msg.url = "whateverurl"

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM