gpt4 book ai didi

node.js - NodeJS https 超时

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:56 26 4
gpt4 key购买 nike

我想在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com