Python Node.js到前端数据发送失败
我希望Node.js get请求将数据从python发送到前端。res.send只被调用过一次。但是,process.stdout.on会被多次调用,从而导致出现“无法设置头”错误。我希望服务器代码一次发送整个数据,而不需要多次调用process.stdout。我做错了什么 我试着使代码同步。但是,它不起作用。此外,我还尝试使用if头未发送的if条件调用process.stdout.on函数一次。这也不允许将python代码的整个结果发送到d3.js中的前端Python Node.js到前端数据发送失败,python,node.js,d3.js,Python,Node.js,D3.js,我希望Node.js get请求将数据从python发送到前端。res.send只被调用过一次。但是,process.stdout.on会被多次调用,从而导致出现“无法设置头”错误。我希望服务器代码一次发送整个数据,而不需要多次调用process.stdout。我做错了什么 我试着使代码同步。但是,它不起作用。此外,我还尝试使用if头未发送的if条件调用process.stdout.on函数一次。这也不允许将python代码的整个结果发送到d3.js中的前端 app.get('/fetch_sl
app.get('/fetch_slider_val/:sliderVal',(req,res)=>{
var sliderVal = req.params.sliderVal
var X = sliderVal.split(" ")
var sites = X[0].split(',')
var date1 = X[1]
var date2 = X[2]
var normKey = X[3]
var normEnt = X[4]
var spawn = require('child_process').spawn;
var process = spawn('python',['sql_data_retrieve.py',sites,date1,date2,normKey,normEnt]);
console.log('1 out')
i = 0
process.stdout.on('data',function(data){
console.log(res.headersSent) #false and becomes true after the next console.log(res.headersSent) gives true. somehow, this function is called again.
res.send(data.toString()) #some data sent not all
console.log(res.headersSent) #true
})
})
我希望将整个data.toString()发送到前端。然而,它只发送了一半,最后我收到了这个错误消息
_http_outgoing.js:470
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/Users/amishr45/PycharmProjects/NewsVis/TrialVis/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/Users/amishr45/PycharmProjects/NewsVis/TrialVis/node_modules/express/lib/response.js:170:12)
at Socket.<anonymous> (/Users/amishr45/PycharmProjects/NewsVis/TrialVis/app_retrieve.js:103:7)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
\u http\u outgoing.js:470
抛出新的ERR_HTTP_头_SENT('set');
^
错误[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头
在ServerResponse.setHeader(_http_outgoing.js:470:11)
位于ServerResponse.header(/Users/amishr45/PycharmProjects/NewsVis/TrialVis/node_modules/express/lib/response.js:771:10)
在ServerResponse.send(/Users/amishr45/PycharmProjects/NewsVis/TrialVis/node_modules/express/lib/response.js:170:12)
在插座上。(/Users/amishr45/PycharmProjects/NewsVis/TrialVis/app_retrieve.js:103:7)
在Socket.emit(events.js:198:13)
在addChunk(_stream_readable.js:288:12)
在readableAddChunk(_stream_readable.js:269:11)
在Socket.Readable.push(_stream_Readable.js:224:10)
at Pipe.onStreamRead[as onread](内部/流基公共空间:94:17)