【问题标题】:Express Serving files from different location on https来自 https 上不同位置的快速服务文件
【发布时间】:2016-03-17 00:40:15
【问题描述】:

我正在运行一个松散地基于 angular-fullstack 应用程序的应用程序。它同时有一个 http 服务器和一个 https 服务器,它们具有相同的 express 应用程序。我的应用程序在我的本地机器上运行良好,在 http 或 https 中,即使我运行 NODE_ENV="production" node server/app.js。但是,当我将 dist 文件夹推送到我的 AWS ec2 实例时,该应用程序仅适用于 http://example.com,不适用于 https。

在 https 地址,我得到Error: ENOENT, stat 'client/index.html' at Error (native)。我没有客户端文件夹,我的客户端代码(例如 index.html)位于名为 public 的文件夹中。我不明白为什么此内容在 http 而不是 https 中正确提供,如果有人可以帮助我,我将不胜感激。

//Server/app.js file
var app = express();

var httpsServer = require('https').createServer(credentials, app);
var httpServer = require('http').createServer(app);
httpsServer.listen(httpsPort, config.ip, function () {
  console.log('https Express server listening on %d, in %s mode', httpsPort, app.get('env'));
});
httpServer.listen(httpPort, config.ip, function () {
  console.log('http Express server listening on %d, in %s mode', httpPort, app.get('env'));
});



var env = app.get('env');
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser());
app.use(passport.initialize());
console.log(env)
if ('production' === env) {
  app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
  console.log(path.join(config.root, 'public'));
  app.use(express.static(path.join(config.root, 'public')));
  app.set('appPath', config.root + '/public');
  app.use(morgan('dev'));
}

if ('development' === env || 'test' === env) {
  console.log('i am overwriting everything');
  app.use(require('connect-livereload')());
  app.use(express.static(path.join(config.root, '.tmp')));
  app.use(express.static(path.join(config.root, 'client')));
  app.set('appPath', 'client');
  app.use(morgan('dev'));
  app.use(errorHandler()); 
}

env 记录为'production'path.join(config.root, 'public') 登录到 /home/ubuntu/www/public/ I am overwriting everything 从不登录。

【问题讨论】:

    标签: node.js express amazon-ec2 angular-fullstack


    【解决方案1】:

    在您推送到服务器后,它看起来可能正在运行您的 development.js。为了防止它读取您的开发,请尝试在您的 package.json 中添加一个启动脚本。

    "scripts": {
        "start": "NODE_ENV="production" node server/app.js"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2018-08-25
      • 1970-01-01
      • 2014-11-17
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多