我使用ssl有效证书
在heroku上部署一个简单的nodejs服务器var https = require('https');
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var app = express();
var cors = require('cors');
var credentials = {
key: fs.readFileSync('./cert/key.key'),
cert: fs.readFileSync('./cert/cert.crt'),
ca: fs.readFileSync('./cert/bundle.crt'),
requestCert: true,
rejectUnauthorized: false
};
var server = https.createServer(credentials, app);
var io = require('socket.io')(server);
app.use(cors());
app.use(bodyParser.json({limit: '12mb'}));
app.use(bodyParser.urlencoded({limit: '12mb', extended: true }));
io.on('connection', function(socket){
console.log(socket);
socket.on('authenticate', function(data){
console.log(data);
});
});
var port = process.env.PORT || 8080;
server.listen(port, function () {
console.log("server listen on", this.address());
});
server.on('clientError', function(err) {
console.log('ERROR', err);
});
问题是,当我启动应用程序时,我收到此错误
ERROR { Error: socket hang up
at TLSSocket.<anonymous> (_tls_wrap.js:820:25)
at emitOne (events.js:101:20)
at TLSSocket.emit (events.js:188:7)
at Socket._handle.close (net.js:492:12)
at Socket.g (events.js:286:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at TCP._handle.close [as _onclose] (net.js:492:12) code: 'ECONNRESET' }
当我尝试通过chrome连接到我的服务器时,我收到此错误 在服务器端
2017-01-05T15:03:43.465542+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/socket.io/?EIO=3&transport=polling&t=LblA3JZ" host=www.myapp.com request_id=51982ce9-99e3-4677-acb4-a0f14eb88999 fwd="95.251.90.218" dyno=web.1 connect=1ms service=2ms status=503 bytes=0
2017-01-05T15:03:43.460178+00:00 app[web.1]: ERROR Error: 140414443095936:error:1407609C:SSL routines:SSL23_GET_CLIENT_HELLO:http request:../deps/openssl/openssl/ssl/s23_srvr.c:394:
和客户端的这个错误
XMLHttpRequest cannot load https://www.myapp.com/socket.io/?EIO=3&transport=polling&t=LblDK25. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.8:8100' is therefore not allowed access. The response had HTTP status code 503.
在第一时刻,我认为这是socket.io但问题仍然存在,正常的REST操作。 现在我认为我是一个SSL问题。我使用godaddy提供程序创建一个有效的证书,并成功将其上传到heroku。事实上,如果我尝试通过https连接连接到我的应用程序,证书是有效的。也许问题是当我尝试将此证书上传到我的nodejs app.someone可以帮助我吗?提前谢谢
答案 0 :(得分:6)
您应该使用HTTP服务器而不是HTTPS。 SSL终止发生在Heroku的负载平衡器上;他们发送您的应用简单(非SSL)流量,因此您的应用应该创建一个非HTTPS服务器。