2017-04-01 52 views
-2

想要一個節點路由器,將POST Json發送到某些遠程API?從節點的託管路由器發佈到遠程API(不是託管節點的API)

今天早上我爲這個問題付出了很多努力,所以我想通過提供一些綜合例子來分享這個好處。

在每個例子中,路由器都有一個GET方法,當被調用時,POSTS回到同一個路由器。我也非常清楚地展示瞭如何發送以及如何訪問接收到的數據。

在Node.js中,在路由器中,您有時可能需要從路由器發送到某個遠程API。

---使用--- npm install needle -save文件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 install request -save文件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; 

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

---當你只想發佈一些Json數據使y我們的生活變得更加簡單的,而不是做的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; 

一個PUT也許最簡單的所有

---使用--- npm install requestify -save文件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; 

享受&我希望這些更全面的演示也能幫助你。

+0

這些都不符合[help] – charlietfl

+0

中概述的本網站的問題/答案格式結構您很想分享知識,但這不是恰當的地方。 –

+0

雖然我同意這個網站上有很多資料需要這種關注,清晰度和幫助 – Steve

回答

-1

看起來不錯。很多努力記錄不同的選項。

稍後我將添加一個Requestify.js示例。請繼續關注並查找以上內容