賞金將在 6 天后到期。此問題的答案有資格獲得 200聲望賞金。 Alexander Mills正在從有信譽的來源尋找答案。
看到這個奇怪的問題,對 http 服務器的請求沒有收到任何資料。我只是想逐行流式傳輸一些 JSON。
const http = require('http');
// server code here:
const s = http.createServer((req, res) => {
console.log('request received.'); // 1
req.on('data', d => {
console.log('hello data:',String(d)); // <--- this line needs to be visited
});
});
s.listen(4444);
// client code below
const r = http.request({
protocol: 'http:',
// host: '0.0.0.0', // default is fine
port: 4444,
timeout: 500
}, res => {
// ignore
res.on('data', d => {
console.log('response from server:', res);
});
});
let requestNum = 1;
(async () => {
while (1) {
console.log('writing:', {requestNum});
const json = JSON.stringify({
timeSent: Date.now(),
requestNum: requestNum
});
r.write(json '\n');
await new Promise(resolve => setTimeout(resolve, 500));
}
})()
所以這條線被擊中:
console.log('request received.'); // 1
但問題是這個回呼永遠不會觸發:
req.on('data', d => {
// we never get here :(
console.log('hello data:',String(d));
});
無法弄清楚為什么。
uj5u.com熱心網友回復:
method請在您的要求中添加
{
protocol: "http:",
// host: '0.0.0.0', // default is fine
port: 4444,
timeout: 500,
method: "POST"
}
如果method未指定,則默認為GET并且通常GET沒有正文。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/530864.html
