var app, certificate, credentials, express, fs, http, httpServer, https, httpsServer, privateKey;
fs = require('fs');
http = require('http');
https = require('https');
privateKey = fs.readFileSync('key.pem', 'utf8');
console.log(privateKey);
certificate = fs.readFileSync('cert.pem', 'utf8');
console.log(certificate);
credentials = {
key: privateKey,
cert: certificate
};
express = require('express');
app = express();
httpServer = http.createServer(app);
httpsServer = https.createServer(credentials, app);
httpServer.listen(80);
httpsServer.listen(443);
我在OS X上,我确认没有其他人在80和443上听。我以sudo
运行,当我去http://127.0.0.1
时,它有效。但是,当我去https://127.0.0.1
时,我找不到。
我做错了什么?
答案 0 :(得分:1)
添加以下代码行:
init
另外,尝试摆脱所有的http模块,因为express会处理大部分内容。查看Hello World for Express的开头,http://expressjs.com/starter/hello-world.html并查找它处理端口的部分。
答案 1 :(得分:0)
要让您的应用分别在端口http
和https
上同时收听80
和443
,请执行以下操作
创建快速应用:
var express = require('express');
var app = express();
express()
返回的应用是JavaScript功能。它可以作为回调来传递给Node的HTTP服务器来处理请求。这样可以使用相同的代码库轻松提供应用的HTTP和HTTPS版本。
您可以按照以下方式执行此操作:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
有关完整详细信息,请参阅doc