app.post('/monitoring/restart', async function (req, res) {
let serviceName = req.query.serviceName;
let serviceType = req.query.type;
let serverName = req.query.serverName;
let forwarded = req.query.forwarded;
try {
if (serverName === process.env.SERVER_NAME) {
let result = await restartService(serviceName, serviceType);
res.status(200).json({
success: true,
result,
});
} else if (!forwarded) {
let matchingServer = OTHER_SERVERS.map(server => server.split(':')).find(server => server[0] === serverName);
const response = await axios.post(`http://${matchingServer[1]}:${config.server.port}/monitoring/restart?serviceName=${serviceName}&type=${serviceType}&serverName=${serverName}&forwarded=true`, {});
res.status(200).json(response.data);
} else {
throw new Error('Service already forwarded');
}
} catch (error) {
logger.error({
description: 'Error in restarting service',
error
});
res.status(500).json({
success: false,
error
});
}
});
因此,当我等待来自axios发布请求的响应时,我会从其他服务器获取响应,但是express不会使用res.json将响应发送回客户端(即使我发送“ hello world”作为响应) )。我在浏览器中得到ERR_EMPTY_RESPONSE。快递请求也被两次调用。要注意的一点是,如果我不等待响应,表示就能发送响应。我还尝试用15秒的setTimeout代替axios请求,以检查是否存在任何超时问题,但这是可行的。也将axios替换为请求库,但无济于事。