I'm reading "Beginning Mobile Application Development in the Cloud" by Richard Rodger and some sample codes use Connect which, I have found, no longer allows direct request processing to the router layer. Here is a sample that gives a 'has no method router' error:
var common = require('./common.js');
var util = common.util;
var connect = common.connect;
var mongo = common.mongo;
var server = connect.createServer(
connect.router(function(app){ //gives the error: "has no method 'router'
// POST {id:<string>}
app.post('/todo/stats/init',function(req,res,next){
common.readjson(req,function(json){
common.sendjson(res,{ok:true,id:json.id});
})
})
// POST {time:<UTC-millis>,total:<todos>,done:<done todos>}
app.post('/todo/stats/collect/:id',function(req,res,next){
var id = req.params.id;
common.sendjson(res,{ok:true,id:id});
common.readjson(req);
})
})
);
mongo.init('todo','localhost');
mongo.open()
server.listen(3000);
Most of the books about node include code that doesn't work and it is very frustrating for someone who begins now to learn and un-learn a minute later. Open Source framework undergoes amazingly fast changes! I just need to learn how to do this the right way. Any thoughts?
Is it right like that?
var common = require('./common.js');
var util = common.util;
var connect = common.connect;
var mongo = common.mongo;
connect.createServer(function (req, res, next) {
app.post('/todo/stats/init',function(req,res,next){
common.readjson(req,function(json){
common.sendjson(res,{ok:true,id:json.id});
})
})
app.post('/todo/stats/collect/:id',function(req,res,next){
var id = req.params.id;
common.sendjson(res,{ok:true,id:id});
common.readjson(req);
})
mongo.init('todo','localhost');
mongo.open();
}).listen(3000);
The connect module you had installed are the newest than the book. Try sollution from this blog: http://blog.riff.org/2012_04_09_missing_the_connect_router_middleware_for_nodejs
or use connect-router module by run command: npm install connect-route
or clone from github: git clone git://github.com/baryshev/connect-route.git
into your node_module directory then do install by: cd connect-route
--> npm install -d