【问题标题】:Mac OS X NodeJS: has no method 'router' errorMac OS X NodeJS:没有方法“路由器”错误
【发布时间】:2012-03-05 17:37:03
【问题描述】:

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

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

    win764:node iwaldman$ node -v
         v0.6.12

使用 npm install connect 安装连接。

写了一个简单的程序,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。

得到以下错误:

    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)

感谢任何想法。

【问题讨论】:

  • 您在使用连接和阅读快速教程吗? connect.router 不是一个东西。 app.get 也不是
  • 我正在学习教程。我没有使用 Express。
  • 什么教程?也许它已经过时了?
  • 好点,先生。有趣的是,它几天前还在工作。也许连接的更新破坏了此功能。非常感谢您的快速回复。
  • 嗨,很遗憾我没有链接,因为我正在阅读“在云中构建应用程序”一书。

标签: node.js connect npm


【解决方案1】:

好吧,这很难说,因为看起来您所遵循的教程确实没有使用 connect,但这里有一个使用 connect 的示例应该可以工作。

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);

【讨论】:

  • 非常感谢。下面是一个教程链接,该教程引用了 connect.router:nodenerd.net/post/2178460914/leveraging-connect。谢谢!
  • 继续安装 Express,npm install express,代码正常工作。我不知道,可能版本的 connect 与 express 打包在一起,或者 express 构建在 connect 上。无论如何,感谢您的快速回复和示例代码。
  • Express 确实使用了连接,您只是使用了一个非常古老且过时的连接教程。 Express 现在使用类似的语法。
【解决方案2】:

安装express并稍微改写代码:

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

function sendjson(res,obj)
{
    res.writeHead(200, {
        'Content-Type': 'application/json',
    });

    var objstr = JSON.stringify(obj);
    util.debug('SENDJSON:' + objstr);
    res.end(objstr);
}


var app = express();

app.get('/foo', function(req,res) {
    sendjson(res, {path:'/foo'});
});

app.get('/bar', function(req,res) {
    sendjson(res, {path:'/bar'});
});

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

【讨论】:

    【解决方案3】:

    我也有同样的问题。 Richard Rodger,“开始在云端开发移动应用程序”的作者建议我应该使用 dispatch 模块 (https://github.com/caolan/dispatch) 或安装旧版本的 connect , 使用:

    npm install git://github.com/senchalabs/connect.git#1.8.6

    希望这会有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 2023-03-29
      • 2013-02-12
      • 2014-10-17
      • 2016-11-23
      相关资源
      最近更新 更多