connect.staticProvider问题

时间:2011-08-20 22:35:06

标签: node.js

我正在尝试这段代码:

var connect = require("connect");
var io = require("socket.io");
var spawn = require("child_process").spawn;

var server = connect.createServer(
    connect.favicon(),
    connect.logger(),
    connect.staticProvider(__dirname + '/public')
);
server.listen(8000);

var socket = io.listen(server, {flashPolicyServer: false});
var tail = spawn("tail", ["-f", "./nohup.out"]);

tail.stdout.on("data", function(data) {
    socket.broadcast(data.toString("utf8"));
});

但是当我尝试运行时,我收到了一个错误:

Nathan-Camposs-MacBook-Pro:log Nathan$ node app.js  

node.js:134  
        throw e; // process.nextTick error, or 'error' event on first tick  
        ^  
TypeError: Object function createServer() {  
  if ('object' == typeof arguments[0]) {  
    return new HTTPSServer(arguments[0], Array.prototype.slice.call(arguments, 1));
  } else {
    return new HTTPServer(Array.prototype.slice.call(arguments));
  }
} has no method 'staticProvider'
    at Object.<anonymous> (/Users/Nathan/Sites/log/app.js:8:10)
    at Module._compile (module.js:407:26)
    at Object..js (module.js:413:10)
    at Module.load (module.js:339:31)
    at Function._load (module.js:298:12)
    at Array.<anonymous> (module.js:426:10)
    at EventEmitter._tickCallback (node.js:126:26)
Nathan-Camposs-MacBook-Pro:log Nathan$ 

1 个答案:

答案 0 :(得分:6)

我真的不知道connect库。但是this documentation说,它是static而非staticProvider(在1.0版本中)。

所以你的服务器创建部分应该是:

var server = connect.createServer(
    connect.favicon(),
    connect.logger(),
    connect.static(__dirname + '/public')
);