1. 程式人生 > >node 請求接口

node 請求接口

function exp .post oca ole resp nod param chan



1、app.js

var express = require(‘express‘)
const bodyParser = require(‘body-parser‘)
const cookieParser = require(‘cookie-parser‘);





var app = express();
app.use(bodyParser())
app.use(cookieParser())



app.get(‘/‘, function(req, res){
console.log(req.query)
res.json({success:100})
})
app.post(‘/‘, function(req, res){
console.log(req.body)
res.json({success:2000})
})




app.listen(3000)

2、client.js

var express = require(‘express‘)
var app = express();
var Client = require(‘node-rest-client‘).Client;
var client = new Client();

app.get(‘/‘, function(req, res){
var args = {
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" }
};
client.get("http://localhost:3000",args, function (data, response) {
console.log(data);
// console.log(response);
});

})
app.get(‘/abc‘, function(req, res){
var args = {
data: { test: "hello" ,user:11111},
headers: { "Content-Type": "application/json" }
};
client.post("http://localhost:3000",args, function (data, response) {
console.log(data);
res.send(data)
// console.log(response);
});

})

app.listen(8888)

3、分別啟動app.js 和 client.js

4、在網站出入locahost:8888

node 請求接口