NodeJS Express-下载静态文件时等待一分钟以上

时间:2018-07-06 12:57:43

标签: node.js express

我有一个使用Express的NodeJS应用,该应用可提供Angular应用。该应用程序以群集模式运行。在本地运行时,它就像是一种魅力。

但是,我在生产环境中使用docker服务应用程序。在生产环境上加载页面时,页面需要花费一分钟以上的时间来加载某些静态文件: Download pending for more than a minute

最终,该页面将被加载,但是它花费的时间比应该加载的时间长。我想知道server.js文件是否有问题,该文件是Express应用程序的主文件。

服务器文件如下:

// Get dependencies
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
        cluster.fork();
    });

    console.log("All workers started.");
}
else {
    const path = require('path');
    const https = require('https');
    const fs = require('fs');
    const cookieParser = require('cookie-parser');
    const bodyParser = require('body-parser');
    const sslPrivateKey = fs.readFileSync('sslcert/cert.key', 'utf8');
    const sslCertificate = fs.readFileSync('sslcert/cert.crt', 'utf8');
    const sslCredentials = { key: sslPrivateKey, cert: sslCertificate, passphrase: 'v1nd1ct4' };
    const express = require('express');
    const compression = require('compression');
    // Get API controller in the form of an Express Router.
    const api = require('./api');
    const portalBackend = require('./portalBackend');

    // Express RequestHandlers.
    const authCallbackHandler = require('./requestHandlers/authCallbackHandler');
    const validateRequestHandler = require('./requestHandlers/validateRequest');
    const responseReadyHandler = require('./requestHandlers/responseReadyHandler');
    const app = express();
    app.use(compression());

    // Serve Angular files as static content.
    app.use(express.static(__dirname + '/dist'));

    // Enable the use of cookies.
    app.use(cookieParser());

    // Enable parsers.
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    // Check if the request is valid.
    app.use(validateRequestHandler);


    // Let Express know of API routes.
    // Enable catching return from API controllers to handle setting new ID cookie.
    app.use('/portalapi', portalBackend, responseReadyHandler);

    app.use('/api', api, responseReadyHandler);

    // Check and handle the request if it originates from the IdentityServer.
    app.use(authCallbackHandler);

    // If no file or /api is matched, redirect to the homepage of the portal.
    app.all('/*', (req, res) => {
        res.sendFile(path.join(__dirname, 'dist'));
    });

    const port = process.env.PORT || '4200';
    app.set('port', port);

    const httpsServer = https.createServer(sslCredentials, app);
    httpsServer.listen(port, '0.0.0.0', () => console.log('HTTP Server listening to port: ' + port));

    console.log(`Worker ${process.pid} started`);
}

我很想听听你们中的任何人对可能出什么问题有任何想法。

0 个答案:

没有答案