【问题标题】:How to access the HTTP request and response objects in METOR application iron router如何在 METOR 应用程序 Iron 路由器中访问 HTTP 请求和响应对象
【发布时间】:2018-07-16 17:58:29
【问题描述】:

我正在开发meteor应用程序,应用程序中有铁路由器。我需要访问路由器中的http请求和响应对象,以访问请求数据。

【问题讨论】:

    标签: http meteor iron-router


    【解决方案1】:

    要访问requestresponse 对象,您需要做的就是告诉Iron Router,该路由是仅服务器路由,方法是通过传递带有{ where: 'server' } 的选项对象,如下所示:

    Router.route('/hello', function () {
      var req = this.request;
      var res = this.response;
      res.end('hello from the server\n');
    }, { where: 'server' });
    

    或者如果你想为不同的方法使用不同的功能:

    Router.route('/hi', { where: 'server' })
      .get(function () {
        var req = this.request;
        var res = this.response;
        res.end('hello from the server\n');
      })
      .post(function () {
        var req = this.request;
        var res = this.response;
        doSomething(req.body)
        res.end('done\n');
      });
    });
    

    在文档中查看更多信息:http://iron-meteor.github.io/iron-router/

    【讨论】:

    • 我正在使用 Router.go('home_private.product', {productID:product });在前端。而在后端 this.route('home_private.product', {path: '/product/:productID', controller: productController });那么在使用 router.go() 方法时如何使用 { where: 'server' }?
    • 我正在这样做但不工作-Router.route('/payment/:bidID', { where: 'server', name:'home_private.wtbPayment', controller: paymentController } )。 post(function () { var req = this.request; var res = this.response; console.log(res); });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多