1. Set up the HTTP server with node. js

1.1 create server. Js

var http = require('http');
var querystring = require('querystring');
http.createServer(function (request, response) {
    console.log('receive request');
    response.writeHead(200, { 'Content-Type': 'text-plain' });
    response.end('Hello World\n');
}).listen(8124);
console.log("node server start ok  port "+8124);Copy the code

1.2 perform server. Js

node server.jsCopy the code

1.3 Starting the Server

As shown in figure:

Request via browser address: http://localhost:8124

2. Send a GET request

2.1 Send get Request To obtain wechat news data through aggregation server

var http = require('http');

var querystring = require('querystring');

http.get('http://v.juhe.cn/weixin/query?key=f16af393a63364b729fd81ed9fdd4b7d&pno=1&ps=10', function (response) {
    var body = [];
    console.log(response.statusCode);
    console.log(response.headers);
    console.log(response);
    response.on('data', function (chunk) {
        body.push(chunk);
    });

    response.on('end', function () {
        body = Buffer.concat(body);
        console.log(body.toString());
    });
});
Copy the code

2.2 perform get_demo. Js

node get_demo.jsCopy the code

2.3 Print the obtained results

3. Send a GET request

3.1 Sending a POST Request Obtain wechat message data from the aggregation server

var http = require('http');

var querystring = require('querystring');

var postData = querystring.stringify({
  'key' : 'f16af393a63364b729fd81ed9fdd4b7d',
  'pno':'1',
  'ps':10
});

var options = {
  hostname: 'v.juhe.cn',
  path: '/weixin/query',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end();Copy the code

3.2 perform post_demo. Js

node post_demo.jsCopy the code

3.3 Print the obtained results