简体   繁体   English

让 Express Static 停止“挂起”节点 14?

[英]Make Express Static stop “pending” with Node 14?

I asked this question half a year ago, but I'm still having the same issue: Express Static network requests stuck on "pending"半年前我问过这个问题,但我仍然有同样的问题: Express Static network requests 卡在“pending”

Tl;dr: Node 14 + Webpack watch mode + Express Static causes the browser to hang every time I change a JS file. Tl; dr:每次更改 JS 文件时,节点 14 + Webpack 观看模式 + Express Static 都会导致浏览器挂起。 This occurs in multiple browsers, after restarting the server, and after clearing the cache.这发生在多个浏览器中,在重新启动服务器后和清除缓存后。

The 2 ways to get the browser to stop hanging are:让浏览器停止挂起的两种方法是:

  • close and reopen the tab关闭并重新打开选项卡
  • go to the homepage (http://localhost) go 到主页(http://localhost)

This issue doesn't occur in Node 12. Is there any way to get the browser to stop hanging in Node 14+? Node 12 中不会出现此问题。有什么方法可以让浏览器停止在 Node 14+ 中挂起?

Edit: Here's my Express code, working on a repo for repro:编辑:这是我的 Express 代码,正在处理 repro 的 repo:

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors({
  origin: HOME_URL,
  optionsSuccessStatus: 200,
}));
app.disable('x-powered-by');

app.use('/api', apiRoutes);
app.use('/sse', sseRoute);

if (process.env.SERVER !== 'production') {
  app.use('/', express.static(
    path.resolve('./build/web'),
    { dotfiles: 'allow' },
  ));

  app.get('*', async (req, res) => {
    const indexFile = await fs.promises.readFile(
      path.resolve('./build/web/index.html'),
    );
    res.send(indexFile.toString());
    res.end();
  });
} else {
  app.all('*', (req, res, next) => {
    if (req.secure || !USE_SSL) {
      next();
    } else {
      res.redirect(`https://${req.hostname}${req.url}`);
    }
  });

  app.use('/', express.static(path.resolve('../web')));

  const indexFile = fs.readFileSync(path.resolve('../web/index.html')).toString();
  app.use('*', async (req, res) => {
    res.send(indexFile);
    res.end();
  });
}

If you are serving SPA as a static content, can you try to change this part:如果您将 SPA 作为 static 内容提供,您可以尝试更改此部分:

app.use('/', express.static(path.resolve('../web')));

  const indexFile = fs.readFileSync(path.resolve('../web/index.html')).toString();
  app.use('*', async (req, res) => {
    res.send(indexFile);
    res.end();
  });

with this:有了这个:

const path = require('path');
const rootPath = path.normalize(__dirname);

// Serve Angular/React frontend files 
app.use('/', express.static(rootPath + '/web', { redirect: false }));

// Rewrite virtual urls to Angular/React app
app.get('*', function (req, res, next) {
    res.sendFile(path.resolve(rootPath + '/web/index.html'));
});

I think it was fixed by changing app.use('/', express.static( to app.use(express.static(我认为通过将app.use('/', express.static(更改为app.use(express.static(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM