2013-04-15 98 views
0

任何人都可以請指出爲什麼我在加載socket.io模塊在客戶端時出現404錯誤?我認爲我在調用layout.jade文件中的socket.io模塊的方式有問題。無法加載已安裝的模塊在玉器客戶端

腳本(類型= '文/ JavaScript的',SRC =」 ../socket.io/socket.io.js')

這是它可以創建插座網絡插座一個基本明確的應用.io模塊。當我使用的客戶端(使用玉模板引擎)連接到網絡插口它throws-

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

這裏我layout.jade,index.jade和app.js.的內容

Layout.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' 

Index.jade

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

Index.js

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

個app.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 

回答

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!'});  
    });