testCallback.js 1.77 KB
/**
 * 
 */
'use strict';

const http = require('http');
const async = require('async');

const hostname = '127.0.0.1';
const port = 3000;

const list = [
   'app.product.refundExchange',
   'app.shopsdecorator.getList',
   'getCategory',
   'h5.product.data',
   'h5.product.intro',
   'sortgroup',
   'web.brand.banner',
   'web.brand.byDomain',
   'web.productBanner.data',
   'web.productComfort.data',
   'web.productModelcard.list',
   'web.productModelTry.data'
];

let client = (path) => {
    return (callback) => {
        let options = {
            hostname: '192.168.102.162',
            port: 80,
            path: path,
            method: 'GET'
        };

        let req = http.request(options, (res) => {
        // console.log(`STATUS: ${res.statusCode}`);
        // console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
            res.setEncoding('utf8');
            let ret = '';
            res.on('data', (chunk) => {
                ret += chunk.toString();
            });
            res.on('end', () => {
                callback(null,JSON.parse(ret));
            });
        });

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

        req.end();
    }
};


const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  let calls = [];

  list.forEach((val,index)=>{
    let call = client(`/${val}.json`);
    calls.push(call);
  });

  async.parallel(calls, (err,datas) => {
      if(err) {
          res.end(err.message);
          return;
      }
      let ret = JSON.stringify(datas);
      res.end(ret);
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});