Using process.stdin as IncomingMessage socket

Viewed 38

I am trying to make a very simple wrapper around nodejs server handlers like (req:IncomingMessage, res:ServerResponse) =>... to use them in a cgi script context.

In a cgi script, the body of the request is given by stdin. I thought this would work:

#!/bin/node
// DOESN'T WORK
const {IncomingMessage} = require('http')

const req = new IncomingMessage(process.stdin)

var body = ""
req.on("data", function(chunk) { body+=chunk})
req.on("close", function() { console.log(body)})

But nothing is echoed from stdin. However, attaching the event handlers to req.socket instead of just req does work:

#!/bin/node
// DOES WORK
const {IncomingMessage} = require('http')

const req = new IncomingMessage(process.stdin)

var body = ""
req.socket.on("data", function(chunk) { body+=chunk})
req.socket.on("close", function() { console.log(body)})

What am I doing wrong? according to the docs IncomingMessage takes a socket as its parameter and stdin is a socket. How do I get the event handlers on the parent req object to read from stdin, as would be the case for an IncomingMessage object created by a http.server? I want this cgi dispatcher to be compatible with handlers that expect to be able to call req.on('data'...).

0 Answers
Related