【问题标题】:Node.js chat on Azure website with SSL使用 SSL 在 Azure 网站上使用 Node.js 聊天
【发布时间】:2017-09-18 07:05:42
【问题描述】:

我使用 Socket.io 创建了一个 Node.js 聊天应用程序。它在本地环境中运行良好。当我使用 SSL 证书在 Azure 网站上进行部署时,出现错误。你们能帮帮我吗?

Server.js:

const connect = require('connect');
const fs = require('fs');

const options = {
  key: fs.readFileSync('./www.reic.vn.key'), 
  cert: fs.readFileSync('./www.reic.vn.crt'),
  ca: fs.readFileSync('./bundle.crt'),
  requestCert: false,
  rejectUnauthorized: false
};

onst express = require("express");
const http = require('https');//.createServer(options);
const socketio = require('socket.io');

const bodyParser = require('body-parser');
const routes = require('./utils/routes'); 
const config = require('./utils/config'); 

const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/chat');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('we are connected!');
});
var chatSchema = mongoose.Schema({
    From:String,
    Message:String,
    To:String,
    FromIdWeb: String,
    ToIdWeb: String,
    FromImgWeb: String,
    ToImgWeb:String,
    IsReaded: {type:String,default:'1'},
    CreatedDate:{type:Date,default:Date.now}
});
var Chat = mongoose.model('Message',chatSchema);
//
class Server{
    constructor(){
        //this.port =  process.env.PORT || 3000;
        this.port = process.env.HTTPS_PORT||3000;
        this.host = `127.0.0.1`;
        this.app = express();
        //this.https = https.Server(options.this.app);
        this.http = http.Server(options,this.app);
        //this.http = http.Server(this.app);
        this.socket = socketio(this.http);
    }
    appConfig(){        
        this.app.use(
            bodyParser.json()
        );
        new config(this.app);
    }
    /* Including app Routes starts*/
    includeRoutes(){
        new routes(this.app,this.socket).routesConfig();
    }
    /* Including app Routes ends*/  
    appExecute(){
        debugger;
        this.appConfig();
        this.includeRoutes();
       this.http.listen(this.port, this.host, () => {
            console.log(`Listening on http://${this.host}:${this.port}`);
        });
    }
}
const app = new Server();
app.appExecute();

端口 3000 是在 Azure 应用程序设置中创建的。

我正在使用 Comodo EssentialSSL 证书。我使用 OPENSSL-Win32 将 Comodo 证书文件生成到 .gem 和 .key 中。

这是我运行命令node sever.js时的结果:

以及来自客户端计算机的此错误消息:

Firefox can’t establish a connection to the server at wss://www.reic.vn/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=SbXYLozbTo28Waa4k6r1fueDrvVEmjTozpydvGbJQqTXSiNdEiSr03svJNXa2D8gp1UwYUt85axpXvcdIn4Lkr7GBjP%2FUYN4%2BmEyu5nO7%2FtwC4SJHuV%2F8LjYzbCs1oJ97MMFKEYLo7kP4eHATVBBtw%3D%3D&connectionData=%5B%7B%22name%22%3A%22hubs%22%7D%5D&tid=10.

希望大家能帮忙。谢谢!

【问题讨论】:

  • 也许你可以尝试在你的 nginx 代理中使用 ssl,而不是你的 express 服务器。

标签: node.js ssl socket.io chat azure-web-app-service


【解决方案1】:

由于您已将应用部署到 Azure Web Apps(也称为 Azure 网站),因此您需要注意以下事项。

1) 您不必通过在 Azure 上手动执行命令 node server.js 来启动在 Azure Web 应用程序上运行的 Node.js 应用程序。由于你的应用根目录下已经有一个入口文件server.js和一个IISweb.config,你可以通过URL直接浏览你的Node.js应用。

2) 您不应该在 Azure 应用程序设置中创建任何端口变量,因为 Azure 使用管道端口来转换 HTTP 请求,并且 Azure Web 应用程序只公开 80443 端口到公共。

请改用以下内容:

this.port =  process.env.PORT || 3000;

3) Socket.IO 使用 WebSockets,在 Azure 上默认不启用。要启用 Web 套接字,请进入 Azure Portal,单击 Web 应用程序刀片中的 Web 应用程序,单击 所有设置 > 应用程序设置。在 Web Sockets 下,点击 On。然后点击保存

更多详情请参考Create a Node.js chat application with Socket.IO in Azure App ServiceSecure your app's custom domain with HTTPS

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    相关资源
    最近更新 更多