Javascript Meteor WebApp中间件:传递参数

Javascript Meteor WebApp中间件:传递参数,javascript,meteor,Javascript,Meteor,我想知道在使用官方软件包侦听特定路由上传入的HTTP请求时如何传递参数 下面是一个示例代码: WebApp.connectHandlers.use("/hello/:myParam", function(req, res, next) { res.writeHead(200); res.end("Your param is", req.myParam); }); 上述类似Express的示例不适用于WebApp。经过一些实验,我现在知道我可以使用req.query访问查询参数。但是We

我想知道在使用官方软件包侦听特定路由上传入的HTTP请求时如何传递参数

下面是一个示例代码:

WebApp.connectHandlers.use("/hello/:myParam", function(req, res, next) {
  res.writeHead(200);
  res.end("Your param is", req.myParam);
});

上述类似Express的示例不适用于WebApp。经过一些实验,我现在知道我可以使用
req.query
访问查询参数。但是WebApp允许您访问常规参数吗?

我不知道有哪种连接中间件可以做到这一点(它可能存在,在这种情况下,您可以插入它),但复制这种行为非常容易:

WebApp.connectHandlers.use("/hello/", function(req, res, next) {
    var parts = req.url.split("/");
    res.writeHead(200);
    res.end("Your param is " + parts[1]);
});

不完全一样,但似乎效果很好。当然,大多数人只会使用iron router来实现类似的功能,但我假设您出于某种原因希望避免使用iron router。

直接使用query,而不是像在express中那样使用Params。除了URL个人偏好使用参数而不是查询之外,没有什么真正的缺点

下面是一个示例,使用ConnectHandler从路由提供PDF:

WebApp.connectHandlers.use("/billPreview", function(req, res, next) {
    var re = urlParse.parse(req.url, true).query;
    if (re !== null) {   // Only handle URLs that start with /url_path/*
      //  var filePath = process.env.PWD + '/.server_path/' + re[1];
      console.log('Re: ',re);
        var filePath = process.env.PWD + '/.bills/pdf/' + re.id +'/'+re.user+'/lastTicket.pdf';
        var data = fs.readFileSync(filePath, data);
        res.writeHead(200, {
            'Content-Type': 'application/pdf'
        });
        res.write(data);
        res.end();
    } else {  // Other urls will have default behaviors
    next();
    }
});

我知道这个问题已经有一年多的历史了,但似乎还没有内置的方法,所以我是这样做的。有一个名为npm的包(我几乎肯定还有其他包)。使用
npm i--save connect route
安装它。然后在代码中:

import connectRoute from 'connect-route';

WebApp.connectHandlers.use(connectRoute(function (router) {
  router.get("/post/:id", function(req, res, next) {
    console.log(req.params); // { id: 1 }

    res.writeHead(200);
    res.end('');
  });
  router.get("/user/:name/posts", function(req, res, next) {
    // etc. etc.
  });
}));
0.1.5版对我来说很有吸引力