简体   繁体   中英

How can I manually create http.IncomingMessage stream?

I want to manually create http.IncomingMessage stream with some header & body. As it is a readable stream, I can't write/pipe data into it. 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.

EDIT:

According to Node.js documentation , extending http.IncomingMessage is possible.

IncomingMessage <http.IncomingMessage> Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage.

How can I extend it? Its constructor asks for 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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