object#server没有方法'使用'

时间:2015-02-27 23:38:13

标签: node.js express

你可以帮我解决下面的问题:) 我使用节点版本0.10.33,npm版本1.4.28和表达版本1.4.28

当我运行以下js代码

var express = require("express"),
         exp = express(),
         http= require('http'); 
    var app = http.createServer(exp),
    io = require('socket.io').listen(app);
//  io = require('socket.io')(app);



var redis = require("redis"),
    rclient = redis.createClient();

app.listen(8080);
//app.use("/static", express.static(__dirname + '/static'));
app.configure(function() {
app.use("/static", express.static(__dirname + '/static'));
})
app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

var board_sockets = {};

io.sockets.on('connection', function (socket) {
    var board_id = socket.handshake.query.board_id;
    if(!board_sockets[board_id]) board_sockets[board_id] = [];
    // TODO: Do something if the board_id is undefined
    board_sockets[board_id].push(socket);
    var redis_key = "board_" + board_id;
    rclient.lrange(redis_key, 0, -1, function(err, res) {
        socket.emit('board_state', res);
    });

    socket.on('update', function (data) {
        for(idx in board_sockets[board_id]) {
            var s = board_sockets[board_id][idx];
            if(s != socket) {
                s.emit('update', data);
                data.timestamp = Math.round(new Date().getTime() / 1000);
                rclient.lpush(redis_key, JSON.stringify(data));
            }
        }
    });
});

我收到了这个错误 object#server没有方法' configure'

1 个答案:

答案 0 :(得分:1)

在您的示例中,apphttp.Server class的一个实例,其原型中确实既没有.configure也没有.use

您可能打算写exp.use(...)而不是app.use(...)

顺便说一句,自{3.1}版快递以来app.configure已被弃用,自快递4.0.0以来已完全删除。