Проблема с кешем изображений Node Express - PullRequest
0 голосов
/ 18 сентября 2018

У меня следующая проблема из журналов Моргана:

Tue, 18 Sep 2018 10:50:26 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:26 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:30 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:30 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:30 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:32 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:32 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:32 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:33 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:33 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:33 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:35 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:35 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:35 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:38 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:38 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:38 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:40 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:40 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:40 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:44 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:44 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:44 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:49 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:49 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:49 GMT GET /public/img/shield_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:51 GMT GET /public/img/healthpack_powerup.png - ::ffff:127.0.0.1 200
Tue, 18 Sep 2018 10:50:51 GMT GET /public/img/speedboost_powerup.png - ::ffff:127.0.0.1 200

Да, те же файлы требуются.и загружается снова и снова.

из консоли браузера:

  • URL запроса: https://127.0.0.1/public/img/speedboost_powerup.png
  • Метод запроса: GET
  • Код состояния: 200 OK
  • Удаленный адрес: 127.0.0.1:443
  • Политика реферера: no-referer-when-downgrade
  • Accept-Ranges: байты
  • Контроль кэша: общедоступный, max-age = 0
  • Соединение: keep-alive
  • Длина содержимого: 2163
  • Тип содержимого: image / png
  • Дата: вт, 18 сентября 2018 11:45:39 GMT
  • ETag: W / "873-165340ec190"
  • Дата последнего изменения: пн,13 августа 2018 16:11:46 GMT
  • X-Powered-By: Express
  • Примите: image / webp, image / apng, image / , / *;q = 0,8
  • Accept-Encoding: gzip, deflate, br
  • Accept-Language: es-419, es; q = 0,9, en-GB; q = 0,8, en; q =0,7
  • Соединение: keep-alive

Cache-Control: общедоступный, max-age = 0 с max-age = 0 не выглядит хорошо.

  • Изображение (асинхронное)
  • Drawing.drawPowerup @ Drawing.js: 175
  • Game.draw @ Game.js: 232
  • Game.run @ Game.js: 96
  • (анонимно) @ base.js: 32

Итак .. Рисунок: 175

Drawing.prototype.drawPowerup = function(coords, name) {
  this.context.save();
  this.context.translate(coords[0], coords[1]);
  var powerup = new Image();
  powerup.src = Drawing.BASE_IMG_URL + name + '.png';
  this.context.drawImage(powerup, -powerup.width / 2, -powerup.height / 2);
  this.context.restore();
};

Источник Drawing.BASE_IMG_URL + name + '.png';Это означает, что ... /public/img/shield_powerup.png

Публичная папка выглядит как сбой при выполнении кэширования, вот как (сервер):

  • // espacios publicos
  • app.use ('/ public', express.static (__dirname + '/ public'));
  • app.use ('/ shared', express.static (__dirname + '/ shared)'));

Нет кэша.Вот моя первая попытка:

  • // espacios publicos
  • app.use ('/ public', express.static (__ dirname + '/ public'), {maxAge: 30000});
  • app.use ('/ shared', express.static (__dirname + '/ shared'), {maxAge: 30000});

Когда яrund:

$ node server.js --dev --free
C:\Users\xxxxx\xxxxx\xxxxx\xxxxx\node_modules\express\lib\router\index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^

TypeError: Router.use() requires a middleware function but got a Object
at Function.use (C:\Users\xxxxx\xxxxx\xxxxx\xxxxx\node_modules\express\lib\router\index.js:458:13)
at EventEmitter.<anonymous> (C:\Users\xxxxx\xxxxx\xxxxx\xxxxx\node_modules\express\lib\application.js:220:21)
at Array.forEach (native)
at EventEmitter.use (C:\Users\xxxxx\xxxxx\xxxxx\xxxxx\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:\Users\xxxxx\xxxxx\xxxxx\xxxxx\server.js:116:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)

Кто-нибудь знает, что я делаю не так?Я исчерпан

Кроме того, я подошел по-другому:

  • const cacheTime = 86400000 * 30;// желаемое время
  • const path = require ('path');

  • // espacios publicos

  • app.use (express.static (path.join (__dirname, 'public'), {maxAge: cacheTime}))
  • app.use (express.static (path.join (__dirname, 'shared')), {maxAge: cacheTime}))

Что работает, однако, есть много ошибок:

127.0.0.1/:1 Refused to apply style from 'https://127.0.0.1/public/bower/bootstrap/dist/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
127.0.0.1/:1 Refused to apply style from 'https://127.0.0.1/public/dist/minified.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/public/bower/jquery/dist/jquery.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/public/bower/bootstrap/dist/js/bootstrap.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/public/js/qrcode.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/public/js/sound.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/shared/Constants.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/shared/Util.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/shared/base.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'https://127.0.0.1/public/js/game/AFK_Kicker.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

Продолжается около 60 файлов.

Есть идеи?

С уважением, Крис


Решил первую часть проблемы, но у меня все еще есть основная:

//espacios publicos
app.use('/public', express.static(__dirname + '/public', { maxAge: '1d' }));
app.use('/shared', express.static(__dirname + '/shared', { maxAge: '1d' }));

Теперь это не имеет никакого смысла для меня ...

  • URL запроса: https://127.0.0.1/public/img/shield_powerup.png
  • Метод запроса: GET
  • Код состояния: 200 OK
  • Удаленный адрес: 127.0.0.1:443
  • Политика реферера: no-referer-when-downgrade

  • Accept-Ranges: байты

  • Cache-Control: public, max-age = 86400
  • Соединение: keep-alive
  • Content-Length: 1987
  • Тип содержимого: image / png
  • Дата: вт, 18 сентября 2018 13:09:26 GMT
  • ETag: W / "7c3-165340ec18f"
  • Последнее изменение: пн, 13 авг. 2018 16:11:46 GMT
  • X-Powered-By: Express

  • Принять: image / webp, image / apng, image / , / *; q = 0,8

  • Accept-Encoding: gzip, deflate, br
  • Accept-Язык: es-419, es; q = 0,9, en-GB; q = 0,8, en; q = 0,7
  • Соединение: keep-alive
  • Cookie: xxx
  • Host: 127.0.0.1
  • Referer: https://127.0.0.1/
  • Пользователь-агент: Mozilla / 5.0 (Windows NT 10.0;Win64;x64) AppleWebKit / 537,36 (KHTML, как у Gecko) Chrome / 69,0,3497,92 Safari / 537,36

~ 2 секунды спустя ~

  • URL-адрес запроса: https://127.0.0.1/public/img/shield_powerup.png
  • Метод запроса: GET
  • Код состояния: 200 OK
  • Удаленный адрес: 127.0.0.1:443
  • Политика реферера: no-referer-when-downgrade

  • Диапазоны принятия: байты

  • Контроль кэша: общедоступный, max-age = 86400
  • Соединение: keep-alive
  • Длина контента: 1987
  • Тип контента: image / png
  • Дата: вт, 18 сентября 2018 г. 13:09:26 GMT
  • ETag: W / "7c3-165340ec18f"
  • Последнее изменение: понедельник, 13 августа 2018 16:11:46 GMT
  • X-Powered-By: Express

  • Принять: изображение / webp, изображение / apng, изображение / , / *; q = 0,8

  • Accept-Encoding: gzip, deflate, br
  • Accept-Language: es-419, es; q = 0,9, en-GB; q = 0,8, en; q = 0,7
  • Соединение: keep-alive
  • Cookie: xxx
  • Хост: 127.0.0.1
  • Ссылка: https://127.0.0.1/
  • Пользователь-агент: Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, как Gecko)) Chrome / 69.0.3497.92 Safari / 537.36

Как видно, Chrome продолжает запрашивать файлы, даже если:

Тот же ETag +MaxAge за 1 день.

...