I'm getting error cannot set headers on express js, I think the problem is have to write setHeader, i was set but stil can't, this my code:
router.get('/cek', (req, res) => {
const child = execFile(commandd, ['-c', 'config', 'GSM.Radio.C0']);
child.stdout.on('data',
function (data) {
value = (JSON.stringify(data));
x = value.split('.');
y = JSON.stringify(x[2])
result = y.replace(/\D/g, "");
res.setHeader('Content-Type', 'text/html');
res.send(result);
}
);
child.stderr.on('data',
function (data) {
console.log('err data: ' + data);
}
);
});
I tired to fixing this error for two days, but still cannot, anybody can help?
dataevent will fire multiple times, yielding one chunk of the text fromstdoutat a time. You seem to be treating it as though it'll pass all the text in one go. Assuming there is relatively little text you can concatenate all the chunks indataevents and then send it on thecloseevent. Other approaches are available depending on the specifics. - skirtleres.sendmethod has been called. You could try to add two things: 1) a return before res.send, so the request of/cekwould be blocking, 2) add a res.send(err) on stderr. In both cases something could trigger your mistake - Federico Ibba