Почему мой ttfb очень высокий, когда мое приложение nodejs очень быстрое? - PullRequest
0 голосов
/ 16 марта 2020

У меня есть nodejs приложение, которое я обслуживаю html файл:

const index = resolve('public/index.html');
const html = fs.existsSync(index) ? fs.readFileSync(index, 'utf-8') : '';
app.get('*', (req, res) => {

  res.set('Content-Type', 'text/html');
  res.send(html);
  res.end();
  // res.sendFile(resolve('public/index.html'));
});

Мое приложение находится в aws: web -> ALB (aws) -> nginx -> nodejs.

Моя проблема в том, что ttfb слишком длинный для каждого ресурса.

Посмотрите на / url: из облачных часов (займет около 1 мс):

enter image description here

и от моих devtools одновременно (около 157 мс):

enter image description here

Вот как я знаю, сколько требуется для обработки запроса:

  const startHrTime = process.hrtime();

  res.on('finish', () => {
    const elapsedHrTime = process.hrtime(startHrTime);
    const elapsedTimeInMs = elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6;
    console.log('%s : %fms', req.path, elapsedTimeInMs);
  });

Итак, почему я получаю этот высокий ttfb и что я могу с этим поделать?

...