2012-03-05 19 views
2

使用Mac端口在MAC OS X上安裝了NodeJS v0.6.12。Mac OS X NodeJS:沒有方法「路由器」錯誤

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

    win764:node iwaldman$ node -v 
     v0.6.12 

使用npm安裝連接安裝連接。

寫一個簡單的程序,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) 

任何想法表示讚賞。

+2

你使用連接和閱讀快速教程? 'connect.router'不是一件事情。 'app.get'也不是' – loganfsmyth 2012-03-05 17:44:38

+0

我正在通過教程學習。我沒有使用Express。 – 2012-03-05 18:11:33

+0

什麼教程?也許它已經過時了? – loganfsmyth 2012-03-05 18:16:45

回答

4

好吧,很難說,因爲它看起來像你正在使用的教程沒有使用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); 
+0

非常感謝。以下鏈接指向connect.router的教程:http://nodenerd.net/post/2178460914/leveraging-connect 。謝謝! – 2012-03-05 20:31:44

+0

提前安裝了Express,npm install express和代碼工作。我不知道,可能版本的連接與express連接打包,或者連接的時候可能是express連接。無論如何,感謝您的快速響應和示例代碼。 – 2012-03-05 20:58:03

+2

Express使用connect,你只是使用了一個非常陳舊和過時的Connect教程.Express現在使用類似的語法。 – 2012-03-06 17:18:38

0

安裝快遞和輕微重寫代碼:

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

我有同樣的問題。 「開始在雲中進行移動應用程序開發」的作者Richard Rodger建議我應該使用調度模塊(https://github.com/caolan/dispatch)或安裝較舊版本的連接,使用:

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

希望這有助於! :)