登录
注册
node.js 学习社区
node request.js 实例

龙大

2015-01-21 15:34

 安装:

npm install request

GET:


var request = require("request");
var options = {
    paream1:'x',
    paream2:'y', 
};
var params = querystring.stringify(options); 
request({
      uri: "http://xxxx/users?"+params,
      encoding:'utf8',
      timeout: 10000, //10 second 
      method: "GET"
}, function(error, response, body) {
      if (!error && response.statusCode == 200) { 
        	res.send(body);
      }
}); 
POST:


var request = require("request");
request({
  uri: "http://xxx/users",
  encoding:'utf8',
  timeout: 10000, //10 second 
  method: "POST",
  form: {
		body1: "xxx" ,
		body2: "yyy"
  }
}, function(error, response, body) {
  if (!error && response.statusCode == 200) { 
    res.send(body);
  }
});
PUT:

var request = require("request");
request({
      uri: "http://xxx/users/1",
      encoding:'utf8',
      timeout: 10000, //10 second 
      method: "PUT",
      form: {
          body1:'xxx',
          body2:'yyy', 
      }
}, function(error, response, body) {
      if (!error && response.statusCode == 200) { 
        res.send(body);
      }
});
DELETE:

var request = require("request");
var options = {
    param1:'xxx',
    param2:'yyy'
};
var params = querystring.stringify(options);
request({
      uri: "http://xxx/users/1?"+params,
      encoding:'utf8',
      timeout: 10000, //10 second 
      method: "DELETE"
}, function(error, response, body) {
      if (!error && response.statusCode == 200) { 
        res.send(body);
      }
}); 

回复 · 0

发表回复

你可以在回复中 @ 其他人