浏览代码

add mysql

Dotos 9 年之前
父节点
当前提交
02cc44271b
共有 9 个文件被更改,包括 106 次插入78 次删除
  1. 1 1
      Dockerfile
  2. 23 20
      app.js
  3. 12 1
      configs/config.js
  4. 36 35
      package.json
  5. 3 3
      routes/v1.js
  6. 21 0
      utils/dbUtils.js
  7. 2 2
      utils/logUtils.js
  8. 0 15
      utils/storageUtils.js
  9. 8 1
      utils/utils.js

+ 1 - 1
Dockerfile

@@ -13,5 +13,5 @@ ENV PORT 80
 EXPOSE 80
 
 # ENTRYPOINT ["node", "app.js"]
-# CMD forever start --minUptime 1000 --spinSleepTime 1000 ./bin/www
+# CMD forever start --minUptime 1000 --spinSleepTime 1000 app.js
 CMD npm start

+ 23 - 20
app.js

@@ -7,7 +7,7 @@ var cookieParser = require('cookie-parser');
 var bodyParser = require('body-parser');
 var moment = require('moment');
 //暂时不需要本地化
-//moment.locale('zh-cn');
+moment.locale('zh-cn');
 //Welcome Page
 var welcome = require('./routes/welcome');
 //日志输出
@@ -21,7 +21,7 @@ var app = express();
 //app.listen(process.env.PORT || 5000);
 
 /***
- * 获取引用网站
+ * 统一过滤,打印日志
  */
 app.all('*', function(req, res, next) {
     res.header("Access-Control-Allow-Origin", "*");
@@ -44,23 +44,27 @@ app.all('*', function(req, res, next) {
         Time: moment().format('YYYY-MM-DD HH:mm:ss.ms')
     };
     /**
-     * 不统计和记录日志的请求:
-     * _detect 估计是DaoCloud 探测机器人
-     * *.css 
-     * favicon.ico
-     * robots.txt
-     * (JS文件可能有特殊统计需求,需要单独判断)
+     * 不记录日志和统计的请求:
+     *      _detect 是 VeryCloud 探测机器人
+     *      *.css 
+     *      favicon.ico
+     *      robots.txt
+     *      图片文件
+     *      (JS文件可能有特殊统计需求,需要单独判断)
      */
-    if (!/\/_detect/.test(originalUrl) && !/.css$/.test(originalUrl) && !/favicon.ico/.test(originalUrl) && !/.robots.txt&/.test(originalUrl)) {
-        if (originalUrl.indexOf('/bing/') > -1 || originalUrl.indexOf('/assets/') > -1) {
-            var err = new Error('这个接口已经改了,请不要在访问这个接口了..., 新的接口:https://bird.ioliu.cn/v1/');
-            err.status = 404;
-            next(err);
-            return;
-        }
+    var filter = /_detect|.css|favicon.ico|robots.txt|.png|.jpg|.gif/i.test(originalUrl);
+    if (!filter) {
+        /**
+         * console.log(req.query):
+         * { 
+         *      url: 'http://www.bing.com/HPImageArchive.aspx?format=js',
+         *      idx: '16',
+         *      n: '1' 
+         * }
+         */
         //暂时屏蔽掉*.js日志的记录
         if (originalUrl.indexOf('.js') === -1) {
-            //如果存在引用,则打印日志
+            //如果存在引用网址,则打印日志
             //if(!!logs['Referer']) 
             logUtils.print(logs);
         }
@@ -78,6 +82,7 @@ app.use(logger('dev'));
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
+//静态文件访问路径
 app.use('/static/', express.static(path.join(__dirname, 'public')));
 
 app.use('/', welcome);
@@ -100,8 +105,7 @@ if (app.get('env') === 'development') {
         res.status(err.status || 500);
         res.send({
             code: err.status,
-            message: err.message,
-            error: err
+            message: err.message
         });
     });
 }
@@ -112,8 +116,7 @@ app.use(function(err, req, res, next) {
     res.status(err.status || 500);
     res.send({
         code: err.status,
-        message: err.message,
-        error: {}
+        message: err.message
     });
 });
 

+ 12 - 1
configs/config.js

@@ -1,3 +1,14 @@
 module.exports = {
-    welcome: 'JsonBird - 业界领先的远程 JSON 代理服务'
+    welcome: 'JsonBird - 业界领先的远程 JSON 代理服务',
+    mysql_dev: {
+        host: 'qdm16395144.my3w.com',
+        user: 'qdm16395144',
+        password: 'abc123456',
+        port: '3306',
+        database: 'qdm16395144_db',
+        connectionLimit: 10,
+        supportBigNumbers: true,
+        multipleStatements: true,
+        insecureAuth: true
+    }
 };

+ 36 - 35
package.json

@@ -1,36 +1,37 @@
 {
-    "name": "JsonBird",
-    "version": "1.0.0",
-    "description": "A Remote Json Agent Service",
-    "private": true,
-    "scripts": {
-        "start": "node ./bin/www"
-    },
-    "repository": {
-        "type": "git",
-        "url": "git+https://github.com/xCss/JsonBird.git"
-    },
-    "keywords": [
-        "JsonBird",
-        "JsonProxy"
-    ],
-    "author": "xCss",
-    "license": "ISC",
-    "bugs": {
-        "url": "https://github.com/xCss/JsonBird/issues"
-    },
-    "readme": "./readme.md",
-    "homepage": "https://github.com/xCss/JsonBird#readme",
-    "dependencies": {
-        "body-parser": "~1.15.1",
-        "cookie-parser": "~1.4.3",
-        "debug": "~2.2.0",
-        "express": "~4.13.4",
-        "express-generator": "^4.13.4",
-        "moment": "^2.15.0",
-        "morgan": "~1.7.0",
-        "pug": "^2.0.0-beta6",
-        "request": "^2.74.0",
-        "serve-favicon": "~2.3.0"
-    }
-}
+  "name": "JsonBird",
+  "version": "1.0.0",
+  "description": "A Remote Json Agent Service",
+  "private": true,
+  "scripts": {
+    "start": "node ./bin/www"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/xCss/JsonBird.git"
+  },
+  "keywords": [
+    "JsonBird",
+    "JsonProxy"
+  ],
+  "author": "xCss",
+  "license": "ISC",
+  "bugs": {
+    "url": "https://github.com/xCss/JsonBird/issues"
+  },
+  "readme": "./readme.md",
+  "homepage": "https://github.com/xCss/JsonBird#readme",
+  "dependencies": {
+    "body-parser": "~1.15.1",
+    "cookie-parser": "~1.4.3",
+    "debug": "~2.2.0",
+    "express": "~4.13.4",
+    "express-generator": "^4.13.4",
+    "moment": "^2.15.0",
+    "morgan": "~1.7.0",
+    "mysql": "^2.11.1",
+    "pug": "^2.0.0-beta6",
+    "request": "^2.74.0",
+    "serve-favicon": "~2.3.0"
+  }
+}

+ 3 - 3
routes/v1.js

@@ -35,7 +35,7 @@ router.get('/', function(req, res, next) {
             } else {
                 return res.send(data);
             }
-        });
+        }, next);
     }
 });
 
@@ -45,8 +45,8 @@ function getJSON(url, callback, next) {
             callback && callback(body);
         } else {
             var error = new Error(err);
-            err.status = 404;
-            next(err);
+            error.status = 404;
+            next(error);
         }
     });
 }

+ 21 - 0
utils/dbUtils.js

@@ -0,0 +1,21 @@
+//https://github.com/mysqljs/mysql#readme
+var mysql = require('mysql');
+//获取数据库配置
+var config = require('../configs/config').mysql_dev;
+//使用连接池
+var pool = mysql.createPool(config);
+//存储
+module.exports = {
+    //添加/修改数据
+    set: function(key, val) {
+
+    },
+    //获取数据
+    get: function(key) {
+
+    },
+    //删除数据
+    del: function(key) {
+
+    }
+};

+ 2 - 2
utils/logUtils.js

@@ -1,6 +1,6 @@
-var storage = require('./storageUtils');
+var storage = require('./dbUtils');
 module.exports = {
-    print : function(params) {
+    print: function(params) {
         var arr = ['-------------------------------------'];
         // var date = new Date();
         // var month = date.getMonth() + 1;

+ 0 - 15
utils/storageUtils.js

@@ -1,15 +0,0 @@
-//存储
-module.exports = {
-    //添加/修改数据
-    set: function(key,val){
-
-    },
-    //获取数据
-    get: function(key){
-
-    },
-    //删除数据
-    remove: function(key){
-        
-    }
-};

+ 8 - 1
utils/utils.js

@@ -1,3 +1,10 @@
 module.exports = {
-
+    urlParams: {
+        get: function(key) {
+            return '';
+        },
+        set: function(param) {
+            return '';
+        },
+    }
 };