node.js - NodeJS https 超时

标签 node.js https

我想在 HTTP 和 HTTPS 上运行我的 Node 应用程序。我在 StackOverflow 上找到了一些我实现的代码,但是当我想访问 https:// URL 时出现超时。 通过 HTTP 的正常方式工作正常。当我想访问 https:// 站点时,我在服务器控制台上没有看到任何错误。当我尝试通过 https://

访问时,我在控制台上没有看到任何内容

服务器在亚马逊平台上运行。

编辑:当我启动服务器时,我将 NODE_ENV 变量设置为“生产”

sudo NODE_ENV=production forever start app.js

有人能看出我在这里做错了什么吗?

var env = process.env.NODE_ENV || 'development';
require('./config/config').set(env);//set configuration
console.log(env);
var config = require('./config/config').config;
var express = require('express');

var debug = require('debug')('juweliergids:server');
var http = require('http');

require('./config/mongoose');
/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(config.port);
var app = express();
var compact = require('./config/compact')(app, config);

require('./config/express')(app, config, compact);
require('./config/routes')(app, config, compact);
app.set('port', config.port);




/**
 * Create HTTP and HTTPS server.
 */
var httpServer = http.createServer(app);
httpServer.listen(port);
httpServer.on('error', onError);
httpServer.on('listening', onHttpListening);

if(env === 'production'){
  var https = require('https');
  var fs = require('fs');

  var privateKey = fs.readFileSync('/etc/letsencrypt/live/domain.com/privkey.pem');
  var certifcate = fs.readFileSync('/etc/letsencrypt/live/domain.com/fullchain.pem');

  var credentials = {
      key: privateKey,
      cert: certifcate,
      rejectUnauthorized:false
  }

  var httpsServer = https.createServer(credentials, app);
  httpsServer.listen(443);
  httpsServer.on('error', onError);
  httpsServer.on('listening', onHttpsListening);
}

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onHttpListening() {
  var addr = httpServer.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
   console.log('HTTP server is listening on ' + bind);
  debug('HTTP server is listening on ' + bind);
}

function onHttpsListening() {
  var addr = httpsServer.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
   console.log('HTTPS server is listening on ' + bind);
  debug('HTTPS server is listening on ' + bind);
}

最佳答案

超时往往表明防火墙存在问题,尤其是当您在日志文件中没有看到任何表明正在建立连接的内容时。

因此请检查您的防火墙是否允许传入连接到端口 443。

关于node.js - NodeJS https 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39485413/

相关文章:

node.js - 如何在使用函数接收到集合的创建事件时更新/插入云 Firestore 中的其他文档

ios - 无法从 Zend 服务器加载图像

c# - 通信安全 : Fiddler intercepts my talks. 如何保护我的应用程序?

javascript - 将文档对象参数传递给javascript中函数的正确方法

javascript - 在测试执行之前从模块加载数据

javascript - 如何使用 Node JS 连接到外部 websocket?

rest - Neo4j SSL 文件是 snakeoil.cert 和 snakeoil.key PEM 文件吗?

ssl - 带有自签名证书的后端

c# - asp.net,使用服务器证书制作httpwebrequest C#

javascript - 添加除访问器之外的函数的语法是什么?