无法在jade客户端中加载已安装的模块

时间:2013-04-15 10:45:30

标签: node.js express pug

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

脚本(type ='text / javascript',src ='.. / socket.io/socket.io.js')

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

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 个答案:

答案 0 :(得分: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!'});      
    });