我在Heroku上部署了一个简单的Node.js服务器,并使用有效的SSL证书。
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应用程序中。有人可以帮我吗?提前致谢。