Dotos 8 anni fa
parent
commit
0af0c103e1
2 ha cambiato i file con 26 aggiunte e 7 eliminazioni
  1. 18 6
      routes/mobile.js
  2. 8 1
      views/welcome.pug

+ 18 - 6
routes/mobile.js

@@ -2,7 +2,7 @@ var express = require('express');
 var request = require('superagent');
 var router = express.Router();
 var base = 'http://apis.juhe.cn/mobile/get?key=9f719ab7014f2cbdc7b394edf70d0f76';
-//var cookie = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' };
+var cookie = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' };
 router.get('/', function(req, res, next) {
     getMobile(req, res, next);
 });
@@ -11,7 +11,7 @@ router.post('/', function(req, res, next) {
 });
 
 function getMobile(req, res, next) {
-    var type = req.query.type || req.body.type;
+    var type = req.query.type || req.body.type || 'json';
     var phone = req.query.phone || req.body.phone;
     var callback = req.query.callback || req.body.callback;
     var url = base + '&phone=' + phone + '&dtype=' + type;
@@ -22,7 +22,19 @@ function getMobile(req, res, next) {
             message: ''
         }
     };
-    request.get(url).set(req.headers).end(function(err, response) {
+    if(!phone){
+        output['status']={
+            code:-1,
+            message:'phone number is empty.'
+        };
+        if (callback) {
+            res.jsonp(output);
+        } else {
+            res.json(output);
+        }
+        return;
+    }
+    request.get(url).set(cookie).end(function(err, response) {
         var body = {};
         if (response && response.text) {
             body = response.text;
@@ -49,13 +61,13 @@ function getMobile(req, res, next) {
                 };
             }
             if (callback) {
-                return res.jsonp(output);
+                res.jsonp(output);
             } else {
-                return res.json(output);
+                res.json(output);
             }
         } else {
             res.header('content-type', 'text/xml; charset=utf-8');
-            return res.send(body);
+             res.send(body);
         }
     });
 }

+ 8 - 1
views/welcome.pug

@@ -59,6 +59,11 @@ block content
                             td 远程代理
                             td https://bird.ioliu.cn/v1
                             td GET/POST
+                        tr
+                            td 远程代理v2 <br>
+                                <code>支持自定义header</code>
+                            td https://bird.ioliu.cn/v2
+                            td GET/POST
                         tr
                             td 笑话
                             td https://bird.ioliu.cn/joke
@@ -84,7 +89,9 @@ block content
     
     footer#footer
         div.inner
-            div.copyright &copy; 2016  
+            div.copyright &copy; 
+                script.
+                    document.write(`2016 - ${(new Date()).getFullYear()} `)  
                 a(href='https://ioliu.cn',target="_blank") 云淡风轻