生活随笔
收集整理的這篇文章主要介紹了
expressjs路由和Nodejs服务器端发送REST请求 - - ITeye博客
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Nodejs創(chuàng)建自己的server后,我們?nèi)绻枰獜目蛻舳死胊jax調(diào)用別的服務(wù)器端的數(shù)據(jù)API的接口,這時(shí)候出現(xiàn)了ajax跨域問(wèn)題。?
一種是利用在客戶端解決跨域問(wèn)題?
這種方案大家可以去網(wǎng)上查查?
另一種方案是在服務(wù)器端去請(qǐng)求別的服務(wù)器,然后將數(shù)據(jù)再返回客戶端.這里就涉及到了:?
ajax請(qǐng)求,expressjs接收請(qǐng)求,Nodejs發(fā)送REST請(qǐng)求。?
我著重寫寫關(guān)于這個(gè)方案的解決方法:?
首先利用express創(chuàng)建路由,接收客戶端發(fā)送的不同請(qǐng)求。?
express路由可以接收get請(qǐng)求和post請(qǐng)求。?
get請(qǐng)求可以去看API,因?yàn)槠綍r(shí)我們可能對(duì)JSON的處理較多,所以用到POST請(qǐng)求較多,我這里主要寫寫post請(qǐng)求。?
客戶端發(fā)送請(qǐng)求:?
客戶端代碼:?
Java代碼?
?
$.ajax({???????????type:?'POST',???????????contentType:?'application/json',???????????url:?'/internaltool/project/peoples',???????????data:?null,???????????async:?false,???????????dataType:?'json',???????????success:function?(data){???????????????result?=?data;???????????},???????????error:?function?()?{???????????????alert("Save?error!");???????????}???????});???????????????$.ajax({???????????????type:?'POST',???????????????contentType:?'application/json',???????????????url:??'/internaltool/project/peopleInfoById',???????????????data:?'{"id":?"811435467"}',???????????????async:?false,???????????????dataType:?'json',???????????????success:function?(data){???????????????},???????????????error:?function?()?{???????????????????alert("Save?error!");???????????????}???????????});??
Nodejs接收客戶端發(fā)送的請(qǐng)求,并且Nodejs服務(wù)器端發(fā)送REST請(qǐng)求別的服務(wù)器端取得數(shù)據(jù)。?
Nodejs服務(wù)器端的代碼:?
Java代碼?
?
var?express?=?require('express'),??????sr??????=?require('./static_require'),??????app?????=?express.createServer();?????????//?linql?2012/08/13?Add???????app.configure(function(){??????????app.use(express.methodOverride());??????????app.use(express.bodyParser());??????????app.use(app.router);??????});??????//?End??var?http?=?require('http');?????exports.init?=?function(here)?{??????app.get('/*.js',?sr.getHandler({??????????searchPaths:?[here]??????}));?????????app.get('/*',?function(req,?res)?{??????????res.sendfile(req.param(0))??????});?????????//?linql?2012/08/13?Add??????//?這種情況是普通請(qǐng)求,不帶有json數(shù)據(jù)處理??????app.post('/internaltool/project/peoples',?function(req,?res)?{??????????//?the?post?options??????????var?optionspost?=?{??????????????host?:?'192.168.1.1',??????????????port?:?'8080',??????????????path?:?'/managesystem/Project/personList',??????????????method?:?'POST'??????????};?????????????//?do?the?POST?call??????????//?服務(wù)器端發(fā)送REST請(qǐng)求??????????var?reqPost?=?http.request(optionspost,?function(resPost)?{??????????????resPost.on('data',?function(d)?{??????????????????res.send(d);??????????????});??????????});?????????????reqPost.end();?????????????reqPost.on('error',?function(e)?{??????????????console.error(e);??????????});??????});?????????app.post('/internaltool/project/peopleInfoById',?function(req,?res)?{??????????//?Request?of?JSON?data??????????//?接收客戶端的JSON數(shù)據(jù)??????????var?reqJosnData?=?JSON.stringify(req.body);?????????????//?do?a?POST?request??????????//?prepare?the?header??????????var?postheaders?=?{??????????????'Content-Type'?:?'application/json;?charset=UTF-8',??????????????'Content-Length'?:?Buffer.byteLength(reqJosnData,?'utf8')??????????};?????????????//?the?post?options??????????var?optionspost?=?{??????????????host?:?'192.168.1.1',??????????????port?:?'8080',??????????????path?:?'/managesystem/Project/personMessageById',??????????????method?:?'POST',??????????????headers?:?postheaders??????????};?????????????//?do?the?POST?call??????????var?reqPost?=?http.request(optionspost,?function(resPost)?{?????????????????resPost.on('data',?function(d)?{??????????????????res.send(d);??????????????});??????????});?????????????//?write?the?json?data??????????//?發(fā)送REST請(qǐng)求時(shí)傳入JSON數(shù)據(jù)??????????reqPost.write(reqJosnData);??????????reqPost.end();??????????reqPost.on('error',?function(e)?{??????????????console.error(e);??????????});??????});??????//?End??};??
關(guān)于expres.js可以參照:?
http://www.csser.com/board/4f77e6f996ca600f78000936?
Nodejs發(fā)送REST請(qǐng)求可以參照:?
http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/?
總結(jié)
以上是生活随笔為你收集整理的expressjs路由和Nodejs服务器端发送REST请求 - - ITeye博客的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。