我使用 建立了一个express
项目express-generator
,现在我正在尝试将其转换为使用 cloudflare SSL Certificate
over https
。
我在 port 上运行我的 express 应用程序443
。
但是,当我然后转到域时,我得到 cloudflare 522 错误,即使在阅读文档后我也无法弄清楚是什么原因造成的。
服务器代码
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '443');
app.set('port', port);
/**
* Create HTTP server.
*/
var fs = require('fs')
var https = require('https');
var options = {
key: fs.readFileSync(__dirname + '/certs/key.pem'),
cert: fs.readFileSync(__dirname + '/certs/cert.pem')
};
var server = https.createServer(options, app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* 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 onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
该部分创建的服务器;
var fs = require('fs')
var https = require('https');
var options = {
key: fs.readFileSync(__dirname + '/certs/key.pem'),
cert: fs.readFileSync(__dirname + '/certs/cert.pem')
};
var server = https.createServer(options, app);