test.js 1.63 KB
/**
 * 
 */
'use strict';

const http = require('http');

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 new Promise((resolve, reject) => {

    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', () => {
            resolve(JSON.parse(ret));
        });
    });

    req.on('error', (e) => {
        reject(e);
        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)=>{
      console.log(`/${val}.json`);
    let call = client(`/${val}.json`);
    calls.push(call);
  });

  Promise.all(calls).then(function(datas) {
      let ret = JSON.stringify(datas);
      res.end(ret);
  })
});

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