node代理网络请求

server.js

 1 var express = require('express')
 2 var axios = require('axios')
 3 var port = 9000;
 4 var app = express();
 5 
 6 var allowCrossDomain = function (req, res, next) {
 7   res.header('Access-Control-Allow-Origin', '*');
 8   next();
 9 };
10 
11 app.use(allowCrossDomain);
12 
13 var apiRoutes = express.Router()
14 apiRoutes.get('/getData', function (req, res) {
15   var url = req.query.reqUrl;
16   axios.get(url, {
17     headers: {
18       authority: 'https://xx.xx.xx/',
19       cookie: 'xxxxxxxxxxxxx'
20     },
21     params: req.query
22   }).then((response) => {
23     res.json(response.data)
24   }).catch((e) => {
25     console.log(e)
26   })
27 })
28 
29 app.use(apiRoutes)
30 
31 module.exports = app.listen(port, function (err) {
32   if (err) {
33     console.log(err)
34     return
35   }
36   console.log('Listening at http://localhost:' + port + '\n')
37 })

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="layui-v2.4.5/css/layui.css">
</head>

<body>
  <div class="container">
    <div class="input-box" style="padding:20px 20px 0 20px;">
      <form class="layui-form layui-row" lay-filter="">
        <div class="layui-input-inline layui-col-xs12 layui-col-sm6 layui-col-md3 layui-col-lg3">
          <input type="text" name="title" required lay-verify="required" placeholder="请输入relatedId" autocomplete="off" class="layui-input" id="input" value="5624669de0f55a5286003b95">
        </div>
        <div class="layui-input-inline" style="padding:5px 0 0 20px;">
          <div class="layui-btn layui-btn-sm layui-btn-normal" id="confirm">搜索</div>
        </div>
      </form>
    </div>
    <div class="table-box" style="padding:0 20px;">
      <table class="layui-table">
        <colgroup>
          <col width="400">
          <col>
        </colgroup>
        <thead>
          <tr>
            <th>错误摘要</th>
            <th>应用版本</th>
            <th>最近发生时间</th>
            <th>最后发生时间</th>
            <th>错误次数</th>
            <th>影响用户数</th>
          </tr>
        </thead>
        <tbody id="templateTbody"></tbody>
      </table>
    </div>
  </div>
  <script id="tmpl" type="text/html">
    {{#  layui.each(d.data, function(index, item){ }}
        <tr>
          <td>{{item.summary}}</td>
          <td>{{item.appVersion}}</td>
          <td>{{item.firstHappenTime}}</td>
          <td>{{item.lastHappenTime}}</td>
          <td>{{item.happenTimes}}</td>
          <td>{{item.affectUsers}}</td>
        </tr>
      {{#  }); }}
      {{#  if(d.data.length === 0){ }}
          暂无数据
      {{#  } }} 
  </script>
  <script src="layui-v2.4.5/layui.js"></script>
  <script src="index.js"></script>
</body>

</html>

index.js

 1 layui.use(['form', 'jquery', 'laytpl', 'layer'], function() {
 2     var form = layui.form;
 3     var $ = layui.jquery;
 4     var laytpl = layui.laytpl;
 5     var layer = layui.layer;
 6 
 7     $('#confirm').on('click', function() {
 8         var id = $.trim($('#input').val());
 9         if (!id) {
10             layer.msg('请输入relatedId');
11             return;
12         };
13         var reqUrl = getUrl(id);
14         sendReq(reqUrl);
15     })
16 
17     function getUrl(id) {
18         var date = new Date().toLocaleString();
19         var date1 = date.split(' ')[0].replace(/\//g, '');
20         var date2 = date.split(' ')[1].replace(/:/g, '');
21         return `https://mobile.umeng.com/ht/api/qb/v1/error/search?versions=&startDay=${date1}+000000&endDay=${date1}+${date2}&exValue=&exField=abstract&relatedId=${id}&errorClass=1&pageSize=120&page=1`;
22     }
23 
24     function sendReq(reqUrl) {
25         console.log(reqUrl);
26         $.ajax({
27             url: 'http://127.0.0.1:9000/getData',
28             data: { reqUrl: reqUrl },
29             success: function(ret) {
30                 if (ret.code === 200) {
31                     var data = ret.data;
32                     data.data = normalize(data.data);
33                     var getTpl = tmpl.innerHTML;
34                     var view = document.getElementById('templateTbody');
35                     laytpl(getTpl).render(data, function(html) {
36                         view.innerHTML = html;
37                     });
38                 } else {
39                     layer.msg('请求错误:' + ret.msg);
40                 }
41             }
42         })
43     }
44 
45     function normalize(data) {
46         $.each(data, function(i, e) {
47             e.firstHappenTime = new Date(e.firstHappenTime).toLocaleString();
48             e.lastHappenTime = new Date(e.lastHappenTime).toLocaleString();
49             if (e.summary.indexOf('\n') > -1) {
50                 e.summary = e.summary.slice(0, e.summary.indexOf('\n'));
51             }
52         })
53         return data;
54     }
55 
56     Date.prototype.toLocaleString = function() {
57         function addZero(num) {
58             if (num < 10)
59                 return "0" + num;
60             return num + '';
61         }
62         return this.getFullYear() + "/" + addZero(this.getMonth() + 1) + "/" + addZero(this.getDate()) + " " +
63             addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
64     };
65 })

 

posted @ 2019-01-05 12:09  dora_zc  阅读(736)  评论(0编辑  收藏  举报