将FROM Node的托管路由器发布到远程API(而不是TO Node的托管API) [英] Posting FROM Node's hosted router to a remote API (not TO Node's hosted API)

查看:142
本文介绍了将FROM Node的托管路由器发布到远程API(而不是TO Node的托管API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要一个将Json POST到某个远程API的节点路由器吗?

今天早上我在这个问题上付出了很多努力,所以我想要通过提供一些全面的例子来分享这一点。

I put a lot of effort into this issue this morning so I wanted to share this by offering some comprehensive examples for your benefit.

在每个例子中,路由器都有一个GET方法,当被调用时,POSTS返回到同一个路由器。我也非常清楚地展示了如何发送和如何访问收到的数据。

In each example the router has a GET method that when called, POSTS back to the same router. I'm also showing, very clearly, how to send AND how to access the received data.

在Node.js中,在路由器中,您可能有时会发布什么内容从路由器到一些远程api。

In Node.js, in a router, you might sometime what to post from the router to some remote api.

---使用 npm install needle -save ---文件 routes / nee.js ---

--- using npm install needle -save --- the file routes/nee.js ---

var express = require('express');
var router = express.Router();
var needle = require('needle');

router.get('/', function (req, resp) {
    var dat = { theGreatest: 'ChuckBerry' };
    var lookbackURL = 'http://' + req.headers.host + req.baseUrl;
    needle.post(lookbackURL, dat, { json: true });
    resp.redirect('/');
});


router.post('/', function (req, resp, next) {
    console.log('body.theGreatest', req.body.theGreatest);
    resp.sendStatus(200);
});


module.exports = router;

---使用 npm安装请求-save ---文件 routes / req.js ---

--- using npm install request -save --- the file routes/req.js ---

var express = require('express');
var router = express.Router();
var request = require('request');

router.get('/', function (req, resp) {
    var dat = { theGreatest: 'ChuckBerry' };
    var lookbackURL = 'http://' + req.headers.host + req.baseUrl;
    request.post(lookbackURL, { json: dat });
    resp.redirect('/');
});


router.post('/', function (req, resp, next) {
    console.log('body.theGreatest', req.body.theGreatest);
    resp.sendStatus(200);
});


module.exports = router;

---使用Node自己的 http.request() - 文件 routes / nodehttp.js ---

--- using Node's very own http.request() -- the file routes/nodehttp.js ---

---当你只希望POST一些Json数据,让你的生活变得更简单,而不是做一个的内容类型= application / json -----

--- When you only want to POST some Json data make your life simpler by instead doing a PUT of the content-type=application/json -----

var express = require('express');
var router = express.Router();
var http = require('http');

router.get('/', function (req, resp) {

    var hst = req.headers.host.split(':');
    var dat = { theGreatest: 'ChuckBerry' };
    var bdy = JSON.stringify(dat);  // you have to take care of this for yourself
    var options = { host: hst[0], port: hst[1], path: req.baseUrl, method: 'PUT'  //PUT!
        , headers: { 'Content-Type': 'application/json' }
    };

    var r = http.request(options);
    r.write(bdy);
    r.end();
    resp.sendStatus(200);
});


router.put('/', function (req, resp) { // PUT. it's a PUT not a POST
    console.log('body[\'theGreatest\']', req.body['theGreatest']); // But here you DON'T have to parse it for yourself.
                 // ^ I'm happy for that even if I am feeling the loss of symmetry.
                 // ^^ At the same this is why your life is easier in a PUT instead of a POST.
    resp.sendStatus(200);
});


module.exports = router;

也许是最简单的

---使用 npm install requestify -save ---文件 routes / rify.js ---

--- using npm install requestify -save --- the file routes/rify.js ---

var express = require('express');
var router = express.Router();
var requestify = require('requestify');

router.get('/', function (req, resp) {
    var lookbackURL = 'http://' + req.headers.host + req.baseUrl;
    requestify.post(lookbackURL, {
        theGreatest: 'ChuckBerry'
    })
    .then(function (res) {
        //res.getBody(); // JSON parsed or XML object
        //res.body; // or get the raw
        res.redirect('/');
    });
});


router.post('/', function (req, resp, next) {
    console.log('body.theGreatest', req.body.theGreatest);
    resp.sendStatus(200);
});


module.exports = router;

享受&我希望这些更全面的演示能够帮助你。

Enjoy & I hope these more comprehensive demonstrations help you too.

推荐答案

看起来不错。记录不同选项需要相当多的努力。

Looks good. Quite a bit of effort documenting the different options.

稍后我将添加一个Requestify.js示例。请继续关注并查看

A little later I'm going to add a Requestify.js example. Stay tuned and look for it above

这篇关于将FROM Node的托管路由器发布到远程API(而不是TO Node的托管API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆