My node app is simple, for a request querying the total customers in mysql databases, i do a block call using await to wait for query to finish. The problem is it can only handle ~75 requests per second, too low.
So i try to return 200 whenever i get the request, telling caller i get the request. Then return the query result when ready, mysql query could take a while.
But not working for me yet, this is the code:
router:
router.get('', controller.getCustomers);
controller:
const getCusomers = (req, res) => {
try {
service.getCustomers(res);
res.write('OK');
//res.send(200); this will end the response before query ends
} catch (err) {
res.end(err);
}
service:
const getCustomers = async (res) => {
customers = await mysqlPool.query('select * from cusomerTable');
res.send(customers);
}
Error: Can't set headers after they are sent to the client.
How to fix this pls ?