I write this code with NodeJS:
const setOutput = (res, req, data) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
}
app.post('/getDb', async (req, res, next) => {
if (!req.body.hasOwnProperty('key')) {
setOutput(res, req, {
error: true,
message: 'You must send key'
})
}
const db = openDb(req.body.key);
if (!db) {
setOutput(res, req, {
error: true,
message: 'Not found key'
})
}
setOutput(res, req, {
error: false,
message: 'DB create successful',
data: {
min: 0.1
}
});
})
This code work fine, but I get this error:
(node:253965) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
How can I stop anything after call setOutput?
Note: I don’t want use switch or a lot of if else



