如何在使用Express / Socket.io的Node.js上使用HTTPS [英] How to use HTTPS on Node.js using Express/Socket.io
问题描述
这是我的https代码:
var httpsPort = 443;
var privateKey = fs.readFileSync(mykeypath');
var certificate = fs.readFileSync(mycertificatepath');
var credentials = {key:privateKey,cert:certificate};
var https = require('https')。服务器(凭证,应用程序);
var io = require('socket.io')(https);
https.listen(httpsPort,function(){
logger.info('listen on *:'+ httpsPort);
});
app.get('/ initGame',function(req,res){
var slots = require('./ slots.json',' utf8');
var userObject = {
address:req.connection.remoteAddress,
userAgent:req.headers ['user-agent']
};
db.getPlayedGames(userObject,function(playingGames){
logger.debug(playingGames);
if(typeof playingGames =='undefined'){
playingGames = 0;
} else {
playingGames = playingGames.games_played;
}
var spinsLeft = 10-playingGames;
res.json({
spinsLeft:spinsLeft,
slots :slot
});
});
});我的客户端的
var myServer =//+ document.domain +:443;
$ .get(myServer +/ initGame,function(data){
totalSpinsLeft = data.spinsLeft;
$('#trysLeft')。text(totalSpinsLeft) ;
Seven.init(data.slots);
})。fail(function(){
setTimeout(function(){
$('#spinner2' ('Fehler bitte neu laden!');
},3000);
});
现在我在我的服务器上收到以下异常:
uncaughtException:缺少PFX或证书+私钥。
编辑:现在正在获取
不良请求
您的浏览器发送了一个此服务器无法理解的请求。
原因:你说的是普通HTTP到启用SSL的服务器端口。
请使用HTTPS方案访问此URL。
如果没有您的密钥和证书文件,我将提供一个使用Express,socket.io和https的例子。
首先我将创建密钥和证书文件,所以在目录下运行下列命令:
下面的命令将生成一个包含RSA密钥的文件。
$ openssl genrsa 1024> file.pem
在此您将被要求输入数据,但您可以留空,直到输入crs生成.pem。
$ openssl req -new -key file.pem -out csr.pem
然后,将创建一个包含SSL证书的file.crt文件。
$ openssl x509 -req -days 365 -in csr.pem -signkey file.pem -out file.crt
所以在我的 app.js
文件,我正在设置和启动服务器通知,我使用文件 file.pem
和 file.crt
生成在最后一步:
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
var options = {
key:fs.readFileSync('./ file.pem'),
cert:fs.readFileSync('./ file.crt')
};
var serverPort = 443;
var server = https.createServer(options,app);
var io = require('socket.io')(server);
app.get('/',function(req,res){
res.sendFile(__ dirname +'/public/index.html');
});
io.on('connection',function(socket){
console.log('new connection');
socket.emit('message','这是来自黑暗的一个消息。');
});
server.listen(serverPort,function(){
console.log('server up and running at%s port',serverPort);
});
然后我的 public / index.html
我正在使用服务器:
<!doctype html>
< html>
< head>
< / head>
< body>
< h1>我还活着!!< / h1>
< script src =https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.js>< / script>
< script>
var URL_SERVER ='https:// localhost:443';
var socket = io.connect(URL_SERVER);
socket.on('message',function(data){
alert(data);
});
< / script>
< / body>
< / html>
然后最后如果您从浏览器访问 https:// localhost
,您将看到一条来自websocket服务器的消息的警报。
Im trying to run my node server with https. I'm using express and socket.io.
This is my code for https:
var httpsPort = 443;
var privateKey = fs.readFileSync(mykeypath');
var certificate = fs.readFileSync(mycertificatepath');
var credentials = {key: privateKey, cert: certificate};
var https = require('https').Server(credentials,app);
var io = require('socket.io')(https);
https.listen(httpsPort, function(){
logger.info('listening on *:' + httpsPort);
});
app.get('/initGame', function (req,res){
var slots = require('./slots.json', 'utf8');
var userObject = {
address : req.connection.remoteAddress,
userAgent : req.headers['user-agent']
};
db.getPlayedGames(userObject,function(playedGames){
logger.debug(playedGames);
if(typeof playedGames == 'undefined' ){
playedGames=0;
}else{
playedGames = playedGames.games_played;
}
var spinsLeft = 10-playedGames;
res.json({
spinsLeft: spinsLeft,
slots: slots
});
});
});
on my client its the following:
var myServer = "//" + document.domain + ":443";
$.get( myServer + "/initGame", function(data) {
totalSpinsLeft = data.spinsLeft;
$('#trysLeft').text(totalSpinsLeft);
Seven.init(data.slots);
}).fail(function(){
setTimeout(function(){
$('#spinner2').text('Fehler bitte neu laden!');
},3000);
});
Right now im getting the following exception on my server:
uncaughtException: Missing PFX or certificate + private key.
EDIT: right now im getting
Bad Request
Your browser sent a request that this server could not understand. Reason: You're speaking plain HTTP to an SSL-enabled server port. Instead use the HTTPS scheme to access this URL, please.
It is hard to test your example without your key and cert files instead I am going to provide an example where I am using Express, socket.io, and https.
First I will create the key and cert files, so inside a directory run the following commands from your terminal:
The command below it is going to generate a file containing an RSA key.
$ openssl genrsa 1024 > file.pem
Here you will be asked to input data but you can leave blank pressing enter until the crs.pem is generated.
$ openssl req -new -key file.pem -out csr.pem
Then a file.crt file will be created containing an SSL certificate.
$ openssl x509 -req -days 365 -in csr.pem -signkey file.pem -out file.crt
So in my app.js
file where I am setting and starting the server notice that I am using the files file.pem
and file.crt
generated in the last step:
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
var options = {
key: fs.readFileSync('./file.pem'),
cert: fs.readFileSync('./file.crt')
};
var serverPort = 443;
var server = https.createServer(options, app);
var io = require('socket.io')(server);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
io.on('connection', function(socket) {
console.log('new connection');
socket.emit('message', 'This is a message from the dark side.');
});
server.listen(serverPort, function() {
console.log('server up and running at %s port', serverPort);
});
and then my public/index.html
where I am consuming the server:
<!doctype html>
<html>
<head>
</head>
<body>
<h1>I am alive!!</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.js"></script>
<script>
var URL_SERVER = 'https://localhost:443';
var socket = io.connect(URL_SERVER);
socket.on('message', function(data) {
alert(data);
});
</script>
</body>
</html>
then finally if you access from the browser at https://localhost
, you will see an alert with a message that is coming from the websocket server.
这篇关于如何在使用Express / Socket.io的Node.js上使用HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!