0

谁能指出为什么在客户端加载socket.io模块时出现 404 错误?我认为我在 layout.jade 文件中调用 socket.io 模块的方式有问题。

脚本(类型='文本/javascript',src='../socket.io/socket.io.js')

这是一个基本的快速应用程序,它使用Socket.io模块创建一个网络套接字。当我使用客户端(使用 Jade 模板引擎)连接到 Web 套接字时,它会抛出 -

GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)

这里是我的 layout.jade、index.jade 和 app.js 的内容。

布局.jade

doctype 5
html
head
title= title
script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js')       
script(type='text/javascript', src='/javascripts/flot/jquery.js')
script(type='text/javascript', src='/javascripts/flot/jquery.flot.js')      
script(type='text/javascript', src='../socket.io/socket.io.js') //script(src='http://'+ domain + ':3000/socket.io/socket.io.js')    
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content'

索引.jade

extends layout
block content
h1= title
p Welcome to #{title}

索引.js

exports.index = function(req, res){  
res.render('index', { title: 'Temperature Monitor',domain: 'localhost'  });
};

应用程序.js

var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path');

var app = express();
var httpServer = http.createServer(app);
var io = require('socket.io').listen(httpServer);

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('/', routes.index);
app.get('/users', user.list);

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

io.sockets.on('connection', function(socket) {
    socket.emit('welcome', {'salutation':'TMP36 Sensor output!'});      
});'

我运行以下命令来创建应用程序。

express homeAutomation
cd homeAutomation
npm install
node app.js

我的文件夹结构的详细信息 -

HomeAutomation
  ->node_modules
    ->express
    ->jade
    ->socket.io    
  ->public
    ->routes
  ->views
    ->index.jade
    ->layout.jade
  App.js
4

1 回答 1

1

您的app.js文件有一些错误的代码。修改如下。

    var express = require('express'),
    routes = require('./routes'),
    user = require('./routes/user'),
    http = require('http'),
    path = require('path');

    var app = express();


    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('/', routes.index);
    app.get('/users', user.list);

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

    var io = require('socket.io').listen(httpServer);
    io.sockets.on('connection', function(socket) {
        socket.emit('welcome', {'salutation':'TMP36 Sensor output!'});      
    });
于 2013-04-15T12:45:55.050 回答