使用babel-node

时间:2016-03-22 09:09:27

标签: 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运行app后,我收到以下错误:

  

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

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

TypeError:_socket2.default不是函数

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"
  }

我做的与页面上写的完全一样。请给我这个案子的建议。感谢

3 个答案:

答案 0 :(得分:0)

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

Users

答案 1 :(得分:0)

你应该将socket.io版本改为至少1.4.5:没关系!

请参阅原始源代码:https://github.com/teropa/redux-voting-server/blob/master/package.json

祝你好运!

答案 2 :(得分:0)

在您的server.js中

import Server from "socket.io";

const startServer = port => {
  console.log(`Listening on ${port}`);
  const io = new Server().attach(port);
};
export default startServer;

在您的index.js中

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

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