Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从节点发布';s托管路由器到远程API(不到节点&x27;s托管API)_Javascript_Json_Node.js_Express - Fatal编程技术网

Javascript 从节点发布';s托管路由器到远程API(不到节点&x27;s托管API)

Javascript 从节点发布';s托管路由器到远程API(不到节点&x27;s托管API),javascript,json,node.js,express,Javascript,Json,Node.js,Express,想要一个将Json发布到某个远程API的节点路由器吗? 今天早上我在这个问题上花了很多精力,所以我想通过提供一些全面的例子来分享这一点,以供大家参考 在每个示例中,路由器都有一个GET方法,当调用该方法时,会将其发回同一路由器。我还非常清楚地展示了如何发送和访问接收到的数据 在Node.js中,在路由器中,有时您可能需要从路由器向某个远程api发布什么 ---使用npm安装needle-save——文件routes/nee.js--- var express = require('express

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

今天早上我在这个问题上花了很多精力,所以我想通过提供一些全面的例子来分享这一点,以供大家参考

在每个示例中,路由器都有一个GET方法,当调用该方法时,会将其发回同一路由器。我还非常清楚地展示了如何发送和访问接收到的数据

在Node.js中,在路由器中,有时您可能需要从路由器向某个远程api发布什么

---使用
npm安装needle-save
——文件
routes/nee.js
---

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安装请求-save
——文件
routes/req.js
---

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;
---使用节点自己的
http.request()
——文件
路由/nodehttp.js
---

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;
---当您只想发布一些Json数据时,通过执行
content-type=application/Json
-----

也许是最简单的

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

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;

享受吧&我希望这些更全面的演示也能帮助您。

看起来不错。相当多的工作记录了不同的选项


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

所有这些都不符合本网站的问答格式结构,如中所述。很高兴你想分享知识,但这不是合适的地方。虽然我同意本网站上有很多材料需要这种关注、清晰,和帮助虽然StackOverflow可能是其流行性和搜索能力/查找能力最重要的存储库,但您还打算将其放置在哪里,并提供尽可能多的潜在帮助?