我想在HTTP和HTTPS上运行我的节点应用。我在StackOverflow上找到了一些我实现的代码,但是当我想访问https://
URL时出现超时。
通过HTTP的正常方式工作正常。当我想访问https://
网站时,我在服务器控制台上看不到任何错误。当我尝试通过https://
服务器正在亚马逊平台上运行。
编辑:当我启动服务器时,我将NODE_ENV
varbele设置为'production'
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);
}
答案 0 :(得分:1)
超时往往表示防火墙问题,尤其是当您没有在日志文件中看到任何表明正在建立连接的内容时。
因此,检查防火墙是否允许到端口443的传入连接。