node.js - 使用 babel-node 时得到 _socket2.default 不是一个函数

标签 node.js socket.io redux

我正在以下页面上学习 Full-Stack Redux:http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html#introducing-the-redux-store

在 server.js 中使用 socket-io 创建新服务器并使用 babel-node 运行应用程序后,出现以下错误:

/home/ubuntu/workspace/src/server.js:15

var io = new _socket2.default().attach(8090);
         ^

TypeError: _socket2.default is not a function

at startServer (server.js:4:16)
at Object.<anonymous> (index.js:5:1)
at Module._compile (module.js:409:26)
at loader (/home/ubuntu/workspace/node_modules/babel-cli/node_modules/babel-register/lib/node.js:126:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ubuntu/workspace/node_modules/babel-cli/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at /home/ubuntu/workspace/node_modules/babel-cli/lib/_babel-node.js:161:27
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/babel-cli/lib/_babel-node.js:162:7)

这是server.js的代码

import Server from 'socket.io';

export default function startServer() {
    const io = new Server().attach(8090);
};

这是index.js的代码

import makeStore from './src/store';
import startServer from './src/server';

export const store = makeStore();
startServer();

这是 package.json 文件:

{
  "name": "chat-example",
  "version": "0.0.0",
  "description": "A chat example to showcase how to use `socket.io` with a static `express` server with `async` for control flow.",
  "main": "server.js",
  "repository": "",
  "author": "Mostafa Eweda <mo.eweda@gmail.com>",
  "scripts": {
    "start": "babel-node index",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive",
    "test:watch": "npm run test -- --watch"
  },
  "devDependencies": {
    "babel-cli": "^6.6.5",
    "babel-core": "^6.7.2",
    "babel-preset-es2015": "^6.6.0",
    "chai": "^3.5.0",
    "chai-immutable": "^1.5.3",
    "mocha": "^2.4.5"
  },
  "babel": {
    "presets": [
      "es2015"
    ]
  },
  "dependencies": {
    "async": "~0.2.8",
    "express": "~3.2.4",
    "immutable": "^3.7.6",
    "redux": "^3.3.1",
    "socket.io": "^0.9.17"
  }

我所做的与页面上写的完全一样。请就这个案例给我一些建议。谢谢

最佳答案

我找到了这个问题的解决方案,我在这里回答以防有人需要。 我相信socket.io不再使用attach函数。相反,他们使用以下代码来创建套接字服务器:

const io = Server(8090);

关于node.js - 使用 babel-node 时得到 _socket2.default 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150188/

相关文章:

javascript - 检测客户端断开连接

java - Java(android)中socket.io的文件描述符?

redux - 重新选择 - 创建一个仅用于获取部分状态的内存选择器是否有意义?

reactjs - 使用 Redux-Saga 如何保存我的身份验证 token ?

redux - Identityserver4 与 redux -oidc 客户端请求访问 token - 但客户端未配置为通过浏览器接收访问 token

node.js - 用户未定义 : Nodejs/Express + Passport

node.js - 使用卡 ID strip 创建 token

socket.io - 当 socket.io 房间空了时,你能触发一个事件吗?

node.js - 如何将 Node.js 中的 .zip/.rar 文件解压缩到文件夹中

node.js - 启动 nodejs : openssl config failed 时出错