[Vuejs]-Cannot set headers after they are sent to client Expressjs router

0👍

As stated by Frederico Ibba, this is usually caused after res.send is sent and there is still data being processed… Your workaround for this may simply be to receive all the data before sending it out using res.send. You can try this.

async function executeCommand() {
    return new Promise((resolve, reject) => {
        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, "");

                resolve(result);
             }
        );

        child.stderr.on('data',
           function (err) { // Renamed data for err for clarification
               reject(err);
           }
        );
    });
}

router.get('/url', async (req, res) => {
    try {
        const result = await executeCommand();
        res.setHeader('Content-Type', 'text/html');
        res.send(result);
    } catch(error) {
        // There was an error. I'm throwing a 500
        res.sendStatus(500);
    }
});

Note that this will be effective only if you are confident that the data is being fired once, as indicated by skirtle

Leave a comment