Mac OS X NodeJS:没有方法“路由器”错误 [英] Mac OS X NodeJS: has no method 'router' error

查看:90
本文介绍了Mac OS X NodeJS:没有方法“路由器”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Mac端口在MAC OS X上安装了NodeJS v0.6.12。

Installed NodeJS v0.6.12 on MAC OS X using Mac Ports.

    win764:node iwaldman$ which node
         /opt/local/bin/node

    win764:node iwaldman$ node -v
         v0.6.12

已安装使用npm install连接

Installed connect using npm install connect.

编写一个简单程序,connectServer.js:

Wrote a simple program, connectServer.js:

    var connect = require('connect');
    var util    = require('util');

    function sendJSON(response, obj) {
        response.writeHead(200, {'Content-Type':'application/json'});
        var objStr = JSON.stringify(obj);
        util.debug('SENDJSON: ' + objStr);
        response.end(objStr);
    }

    var server = connect.createServer(
    connect.router(function(app){
        app.get('/foo', function(req, res){
            sendJSON(res, {path: 'foo'});
        })
        app.get('/bar', function(req, res){
            sendJSON(res, {parth: 'bar'});
        })
    })
    );

    server.listen(3000);

    util.debug('Server running at http://127.0.0.1:3000');

运行节点connectServer.js。

Run node connectServer.js.

得到以下错误:

    win764:node iwaldman$ node connectserver.js 

    node.js:201
            throw e; // process.nextTick error, or 'error' event on first tick
                  ^
    TypeError: Object function createServer() {
      function app(req, res){ app.handle(req, res); }
      utils.merge(app, proto);
      utils.merge(app, EventEmitter.prototype);
      app.route = '/';
      app.stack = [].slice.apply(arguments);
      return app;
    } has no method 'router'
        at Object.<anonymous> (/Users/iwaldman/dev/node/connectserver.js:12:10)
        at Module._compile (module.js:441:26)
        at Object..js (module.js:459:10)
        at Module.load (module.js:348:31)
        at Function._load (module.js:308:12)
        at Array.0 (module.js:479:10)
        at EventEmitter._tickCallback (node.js:192:40)

任何想法都会受到赞赏。

Any ideas are appreciated.

推荐答案

好吧,很难说,因为它看起来像是您正在使用的教程并未使用 connect ,但这是一个使用connect的示例,该示例应该有效。

Alright, it's hard to say since it really looks like the tutorial you are following isn't using connect, but here is an example using connect that should work.

function sendJSON(response, obj) {
  response.writeHead(200, {'Content-Type':'application/json'});
  var objStr = JSON.stringify(obj);
  response.end(objStr);
}

function get(path, cb) {
  return function(req, res, next) {
    if (req.method != 'GET' || req.url != path) return next();
    cb(req, res, next);
  }
}

var connect = require('connect')
var app = connect()
  .use(connect.query())
  .use(get('/foo', function(req, res, next) {
    sendJSON(res, {path: 'foo'});
  }))
  .use(get('/bar', function(req, res, next) {
    sendJSON(res, {parth: 'bar'});
  }))
  .listen(3000);

这篇关于Mac OS X NodeJS:没有方法“路由器”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆