node . js /表達。js將POST請求轉發到另一個域

[英]Node.js/Express.js forward POST request to another domain


I have a route in my express.js app on a domain example1.com:

我的快遞有一條路線。域名example1.com上的js應用程序:

router.post('/messages/add', (req, res) => {
  let message = new Message();
  message.title = req.body.title;
  message.body = req.body.body;
  message.save(err => {
    if(err) {
      return res.json({ success: false });
    } else {
      return res.json({ success: false });
    }
  });
});

How do I pass the message to another express app's endpoint on a domain example2.com?

如何將消息傳遞到域example2.com上的另一個express應用程序端點?

UPDATE

更新

Updated the code using the request module suggested by YouneL. example2.com receives an empty object:

使用YouneL建議的請求模塊更新代碼。example2.com收到一個空對象:

UPDATE 2

更新2

Finally got it working. Had to put form:formData instead of formData:formData in the request.post() options.

終於找到了工作。必須在request.post()選項中放入form:formData而不是formData:formData。

Server/domain 1:

服務器/域1:

router.post('/messages/add', function (req, res) {
  let formData = {
    title: req.body.title,
    body: req.body.body
  }

  request.post({
    url:'http://example2.com/api/messages/add',
    form:formData}, function optionalCallback(err, httpResponse, body) {
      if (err) {
        return console.error('upload failed:', err);
      }
      console.log('Upload successful!  Server responded with:', body);
    });
});

Server/domain 2:

服務器/域2:

router.post('/contacts/add', function (req, res) {
    let message = new Message(); // Mongoose model
    message.title = req.body.title;
    message.body = req.body.body;        

    req.checkBody('title', 'Title is required').notEmpty();
    req.checkBody('body', 'Body is required').notEmpty();
    let errors = req.validationErrors();
    if (errors) {
        res.json({ success: false, msg: errors });
    } else {
        contact.save(function (err) {
            if (err) {
                console.log(err);
                res.json({ success: false, msg: 'Failed to add message' });
            } else {
                res.json({ success: true, msg: 'Message added' });                  
            }
        });
    }
});

1 个解决方案

#1


3  

You could use request module to send another post request to example2.com, here is an example:

您可以使用request模塊向example2.com發送另一個post請求,這里有一個示例:

router.post('/messages/add', (req, res) => {

    let message = new Message();
    message.title = req.body.title;
    message.body = req.body.body;
    message.save(err => {

        if (err) {
            return res.json({ success: false });
        }

        // post data to example2.com
        request.post({ url:'example2.com', form: req.body }, (err, httpResponse, body) => { 

            if (err) {
                return res.json({ success: false, msg: 'cannot post to example2.com' });
            }

            res.json({ success: true });

        });


    });

});

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/02/04/1dff8894b1986feb2a680b2095226da2.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com