簡體   English   中英

在meteor.js 中處理發布數據的簡單方法?

[英]Easy way to handle post data in meteor.js?

我需要在我的meteor.js 應用程序中處理一些POST 數據,有沒有一種簡單的方法可以做到這一點?

非常基本,如果它是一個 PHP 應用程序,我只想要 $_POST 變量。

流星路由器

https://github.com/tmeasday/meteor-router#server-side-routing

Meteor.Router.add('/items/:id', 'POST', function(id) {
  // update Item Function
  return [200, 'ok'];
});

如果您只是想攔截GET和POST數據,然后以一種快樂的方式發送Meteor,則可以在服務器上執行類似的操作。

if (Meteor.isServer) {

  var connect = Npm.require('connect');
  var app = __meteor_bootstrap__.app;
  var post, get;

  app
    // parse the POST data
    .use(connect.bodyParser())
    // parse the GET data
    .use(connect.query())
    // intercept data and send continue
    .use(function(req, res, next) {
      post = req.body;
      get = req.query;
      return next();
    });

  Meteor.startup(function() {
    // do something with post and get variables
  });

}

編輯13/01/13

我最終為此(為我自己)創建了一個智能程序包。 沒有文檔,但是歡迎您使用它。 https://github.com/johnnyfreeman/request-data

要獲取foo請求變量:

RequestData.get('foo') // --> 'bar'
RequestData.post('foo') // --> 'bar'

如果找不到該鍵,則這兩種方法都將throw Meteor.Error ,因此,如果該變量是可選的,請確保使用try / catch換行。

您可以在此處使用Meteor的Iron Router ,因為Router (如上所述)已過時且可能不再起作用。

Router.route('/items/:id', {where: 'server'})
    .get(function () {
        this.response.end('get request\n');
    })
    .post(function () {
        this.response.end('post request\n');
    });

我正在使用這個包來序列化正文數據: simple:json-routes 這是鏈接。

這個代碼片段來訪問它:

WebApp.connectHandlers.use('/api/request', (req, res, next) => {
    console.log(req.body);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM