I'm having the following problem:
node:_http_outgoing:576
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:371:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
...
But I couldn't solve it, can you help me?
Code:
const express = require("express");
const app = express();
const port = 3001;
const { spawn } = require('node:child_process');
const ls = spawn('adb', ['logcat']);
app.get("/", (req, res) => {
res.setHeader('Content-Type', 'application/json');
let json = {}
ls.stdout.on('data', (data) => {
const [date, time, pid, pid2, priority, ...other] = String(data).replace(/\s+/g, ' ').split(" ");
json = {
date,
time,
pid: `${pid}-${pid2}`,
priority,
other: other.filter(Boolean)
}
console.log("---S");
console.log(`${data}`);
console.log(json);
console.log("---E\n");
res.json(json);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
res.json(json);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});