我如何修复connect.router?

时间:2012-08-14 10:59:21

标签: node.js routing express connect

我正在通过Node for Front End devs工作,而且当SO上的人已经pointed out时,Connect不再有用于路由的模块。有些人建议使用Express,但我不确定确切的语法。

我正在处理的例子在这里举行:
github.com/garann/node-for-frontend-devs/blob/master/03-03.js

我反过来想完成关于模板的教程:
js: github.com/garann/node-for-frontend-devs/blob/master/04-02.js
html: github.com/garann/node-for-frontend-devs/blob/master/public/parent.html
并且想知道人们是否认为放弃这些基于Connect的tutes并且学习如何使用Express可能会更好?因为路由很可能还需要Express ..

由于SO优秀的垃圾邮件保护,我不得不删除github链接的https://部分。

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

尝试使用此:https://github.com/baryshev/connect-route

<强>更新

在项目文件夹中执行:

npm install connect-route

更新了您示例中的代码:

var connect = require("connect");
var connectRoute = require("connect-route");

connect(
  connect.static(__dirname + "/public"),
  connectRoute(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);

将浏览器指向:

的http:// [your_host_here]:8000 / sayHello的/缺口/名称

答案 1 :(得分:0)

摘要

版本2.x中删除了

connect.router 。我从1.x获取代码并将其发布为connect_router

安装

npm install --save connect_router

用法

if (!connect.router) {
  connect.router = require('connect_router');
}

function route(rest) {
  rest.get('/api/hello', function (req, res) {
    res.end('hello');
  });
}

app = connect()
  .use(connect.router(route))
  ;

server = app.listen(port, function () {
  console.log('listening on', server.address());
});

在Github上

https://github.com/coolaj86/connect_router取自Connect 1.x's router,文档基本上为the tests

原始文档是......某处。