feathersjs -> socketio https 请求不工作

feathersjs -> socketio https request not working

我有一个用 featherjs 制作的应用程序,我想 运行 使用 https。我已经开始工作了。我通过将 'index.js' 文件更改为如下所示来做到这一点:

const fs = require('fs');
const https = require('https');
const app = require('./app');
const port = app.get('port');
const host = app.get('host');
//const server = app.listen(port);
const server = https.createServer({
    key: fs.readFileSync('./certs/aex007.key'),
    cert: fs.readFileSync('./certs/aex007.crt')
}, app).listen(port, function(){
    console.log("Mfp Backend started: https://" + host + ":" + port);
});

一旦我现在去例如。 'https://127.0.0.1/a_service_name' 在邮递员中,我在接受证书后得到结果。当我在浏览器中访问该地址时,它也会给出结果,证书指示是 'red' 因为它是自签名的。

所以我的问题如下。当我在浏览器中转到“http://127.0.01”时,我没有得到任何 'socket' 信息,而是一个空白页面,而不是 'index.html' 文件。我在控制台中收到以下错误

info: (404) Route: /socket.io/?EIO=3&transport=polling&t=LwydYAw - Page not found

那么我正在使用的 'index.html' 文件当前包含以下内容:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script type="text/javascript" src="//cdn.rawgit.com/feathersjs/feathers-client/v1.1.0/dist/feathers.js"></script>
<script type="text/javascript">
    var socket = io('https://127.0.0.1:3001');
    var client = feathers()
        .configure(feathers.hooks())
        .configure(feathers.socketio(socket));
    var todoService = client.service('/some_service');

    todoService.on('created', function(todo) {
        alert('created');
        console.log('Someone created a todo', todo);
    });

</script>

有人可以向我解释如何才能收到警报消息吗?

编辑 2017/09/27 我在网上查到socket.io是这样配置的

var https = require('https'),     
    fs =    require('fs');        

var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt')
};
var app = https.createServer(options);
io = require('socket.io').listen(app);     //socket.io server listens to https connections
app.listen(8895, "0.0.0.0");

但是羽毛的要求-socket.io 是在app.js 而不是index.js。我想知道我是否可以移动它?

作为daffl pointed out on the feathers slack channel here; check out the documentation which requires in feathers-socketio explicitly before calling configure on the app, in addition to the https portion of the docs。把这两个放在一起,我会做这样的事情(未经测试):

const feathers = require('feathers');
const socketio = require('feathers-socketio');
const fs = require('fs');
const https = require('https');


const app = feathers();
app.configure(socketio());

const opts = {
  key: fs.readFileSync('privatekey.pem'),
  cert: fs.readFileSync('certificate.pem')
};

const server = https.createServer(opts, app).listen(443);

// magic sauce! Socket w/ ssl
app.setup(server);

app.jsindex.js 的结构完全由您决定。您可以如图所示在单个文件中执行上述所有操作,或者将 https/fs 要求拆分为 index.js,然后将应用程序配置为 app.js - 我推荐这种方法,因为它会如果您决定使用像 nginx 这样的反向代理来处理 ssl 而不是节点,则允许您更改(通常较小的)index.js 文件。