3

我遇到了 Express.io 的问题,我尝试创建一个简单的 tchat。但我无法包含 socket.io.js,我遇到了一个错误......

我刚刚在我的新 Express 项目中安装了 Express.io。

我的错误:

  1. 获取 http:// * ** /socket.io/socket.io.js 404(未找到)
  2. localhost:1 Uncaught ReferenceError: io is not defined

索引.jade

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    block content
    script(src="http://localhost:3000/socket.io/socket.io.js")
    script(src="/javascripts/user.js")

应用程序.js

/**
 * Module dependencies.
 */

var express = require('express.io')
    , index = require('./routes/index.js')
    , http = require('http')
    , path = require('path');

var app = express();

app.http().io();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', index.index);

app.io.route('ready', function(req) {
    req.io.emit('talk', {
        message: 'io event from an io route on the server'
    });
});

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

index.js(路由)

exports.index = function(req, res){
    res.render('index', {
        title: 'TCHAT'
    });
};

用户.js

io = io.connect();

// Emit ready event.
io.emit('ready');

// Listen for the talk event.
io.on('talk', function(data) {
    alert(data.message);
});

新错误

加载资源失败:服务器响应状态为 404 (Not Found) http:// * :3000/socket.io.js Uncaught ReferenceError: io is not defined

4

1 回答 1

9

伙计,您在http服务器而不是express.io服务器上收听。改变这个

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

对此:

app.listen(app.get('port'), function(){
   console.log("Express server listening on port " + app.get('port'));
});

那么您将能够访问socket.io.js

于 2013-03-15T06:07:40.790 回答