我使用nodejs connent模块代码作为样本。 app.js遵循这个:
var connect = require("connect");
connect(
connect.static(__dirname + "/public"),
// create a router to handle application paths
connect.router(function(app) {
app.get("/sayHello/:firstName/:lastName", function(req, res) {
var userName = req.params.firstName + " " + req.params.lastName,
html = "<!doctype html>" + "<html><head><title>Hello " + userName + "</title></head>" + "<body><h1>Hello, " + userName + "!</h1></body></html>";
res.end(html);
});
})
).listen(8000);
节点app.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/yangzhaojie/tech/nodejs/devsample/app4.js:6: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)
os:Mac OS X Lion 10.7.3
节点:0.6.11
connent:2.1.3
答案 0 :(得分:1)
您必须使用旧教程或其他内容。路由器中间件在不久前从Connect中删除:https://github.com/senchalabs/connect/issues/262
您可以使用其他模块(connect-router)或切换到Express。