【发布时间】:2021-10-29 16:05:57
【问题描述】:
我有一个在端口 9090 上侦听的 http 服务器 - 像这样将请求传送到标准输出:
let server = http.createServer((req, res) => {req.pipe(process.stdout)})
server.listen(9090)
当我像这样用 curl 发送一些东西时:
curl -XGET -T - 'http://localhost:9090' < /tmp/text-input
它有效,我在服务器终端上看到了输出
但是当我在节点中尝试以下操作时:
const http = require('http')
const nurl = new URL("http://localhost:9090")
let request = http.request(nurl)
request.on('response', (res) => {
process.stdin.pipe(request)
})
request.end() // If I emit this, nothing happens. If I keep this, I get the below error
并尝试像这样运行它:node request.js < /tmp/text-input,我收到以下错误:
node:events:368
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at write_ (node:_http_outgoing:748:11)
at ClientRequest.write (node:_http_outgoing:707:15)
at ClientRequest.<anonymous> (/home/tomk/workspace/js-playground/http.js:17:7)
at ClientRequest.emit (node:events:390:28)
at HTTPParser.parserOnIncomingClient (node:_http_client:623:27)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
at Socket.socketOnData (node:_http_client:487:22)
at Socket.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:324:12)
Emitted 'error' event on ClientRequest instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
我想以与curl -T - 相同的方式将我的标准输入传送到http 服务器。我的请求代码有什么问题?
【问题讨论】: