我尝试使用https运行我的节点服务器。我正在使用express和socket.io。
我在控制台“ net :: ERR_CERT_AUTHORITY_INVALID”出现错误
我有自签名SSL。
在没有SSL的情况下运行时,代码可以正常工作。我不知道我的签名证书是否可以接受。
请告诉我如何:1.接受自签名SSL证书。或2.帮助我以不同的方式完成这项工作。
节点v6.16.0 服务器CentOS
这是我的https代码:
//server-side
'use strict';
var https = require('https');
var fs = require('fs');
var path = require('path');
var app = require('express')();
var md5 = require('md5');
var port = process.env.PORT || 3030;
var credentials = {
key: fs.readFileSync('/var/www/httpd-cert/www-root/site.com.key', 'utf8'),
cert: fs.readFileSync('/var/www/httpd-cert/www-root/site.com.crt', 'utf8'),
requestCert: true,
rejectUnauthorized: false
};
var httpsServer = https.createServer(credentials, app);
var io = require('socket.io')(httpsServer);
io.on('connection', function(socket){
console.log('an user connected');
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
httpsServer.listen(port, function(){
console.log('listening on *:3030');
});
客户端
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/Assets/js/socket.io.js"></script>
<script type="text/javascript">
$(function () {
var host = 'https://example.com:3030';
var socket = io.connect(host, {secure: true});
socket.on('connect', function(){
socket.on('event', function(data){});
socket.on('disconnect', function(){});
});
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>