安装npm和socket.io时出错?

时间:2015-05-10 09:48:09

标签: javascript json node.js

我已经成功安装了node.js,我在新目录中创建了package.json和app.js文件,但是当我运行http://localhost:3000/时,我的网页无法使用。

这是我的package.json文件:

{
    "name": "App",
    "version": "0.0.1",
    "description": "App",
    "dependencies": {
        "socket.io": "latest"
    },
    "author": "developer"
}

我的app.js文件:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(3000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

你能告诉我我在哪里做错了吗?

0 个答案:

没有答案