Dotos 9 سال پیش
والد
کامیت
4dd410ecdc
5فایلهای تغییر یافته به همراه68 افزوده شده و 30 حذف شده
  1. 16 4
      app.js
  2. 6 4
      package.json
  3. 13 0
      readme.md
  4. 30 22
      routes/v1.js
  5. 3 0
      utils/utils.js

+ 16 - 4
app.js

@@ -15,10 +15,23 @@ var v1 = require('./routes/v1');
 
 var app = express();
 
+/***
+ * 获取引用网站
+ */
+app.all('*', function(req, res, next) {
+    // res.header("Access-Control-Allow-Origin", "*");
+    // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
+    // res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
+    // res.header("X-Powered-By", '3.2.1');
+    // res.header("Vary", "Origin");
+    // res.header("Content-Type", "application/json;charset=utf-8");
+    console.log('ref:' + req.header('referer'));
+    next();
+});
 // view engine setup
 app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'pug');
-
+app.enable('trust proxy');
 // uncomment after placing your favicon in /public
 //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
 app.use(logger('dev'));
@@ -27,7 +40,6 @@ app.use(bodyParser.urlencoded({ extended: false }));
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));
 
-
 app.use('/', home);
 app.use('/user', index);
 app.use('/v1', v1);
@@ -46,7 +58,7 @@ app.use(function(req, res, next) {
 if (app.get('env') === 'development') {
     app.use(function(err, req, res, next) {
         res.status(err.status || 500);
-        res.render('error', {
+        res.send({
             message: err.message,
             error: err
         });
@@ -57,7 +69,7 @@ if (app.get('env') === 'development') {
 // no stacktraces leaked to user
 app.use(function(err, req, res, next) {
     res.status(err.status || 500);
-    res.render('error', {
+    res.send({
         message: err.message,
         error: {}
     });

+ 6 - 4
package.json

@@ -1,6 +1,7 @@
 {
-    "name": "JsonProxy",
+    "name": "JsonBird",
     "version": "1.0.0",
+    "description": "A Remote Json Agent Service",
     "private": true,
     "scripts": {
         "start": "node ./bin/www",
@@ -8,18 +9,19 @@
     },
     "repository": {
         "type": "git",
-        "url": "git+https://github.com/xCss/JsonProxy.git"
+        "url": "git+https://github.com/xCss/JsonBird.git"
     },
     "keywords": [
+        "JsonBird",
         "JsonProxy"
     ],
     "author": "xCss",
     "license": "ISC",
     "bugs": {
-        "url": "https://github.com/xCss/JsonProxy/issues"
+        "url": "https://github.com/xCss/JsonBird/issues"
     },
     "readme": "./readme.md",
-    "homepage": "https://github.com/xCss/JsonProxy#readme",
+    "homepage": "https://github.com/xCss/JsonBird#readme",
     "dependencies": {
         "body-parser": "~1.15.1",
         "cookie-parser": "~1.4.3",

+ 13 - 0
readme.md

@@ -0,0 +1,13 @@
+# readme
+
+# JsonBird
+> A JSON Remote Agent Service | 一个远程JSON代理服务
+
+## Version 1.0.0
+
+## How to use
+> https://api.ioliu.cn/v1/?url=http[s]://YouWantProxyJSONUrls.com&[?]params1=val1&params2=val2
+
+## You can got
+1. Source station data based on cross domain 
+2. Lets not support JSONP source station data support JSONP

+ 30 - 22
routes/v1.js

@@ -1,38 +1,46 @@
 var express = require('express');
 var request = require('request');
 var router = express.Router();
-router.all('*', function(req, res, next) {
-    res.header("Access-Control-Allow-Origin", "*");  
-    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  
-    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
-    res.header("X-Powered-By",'3.2.1');
-    res.header("Vary","Origin");
-    res.header("Content-Type", "application/json;charset=utf-8");  
+
+router.all('/v1/*', function(req, res, next) {
+    res.header("Access-Control-Allow-Origin", "*");
+    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
+    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
+    res.header("X-Powered-By", '3.2.1');
+    res.header("Vary", "Origin");
+    res.header("Content-Type", "application/json;charset=utf-8");
     next();
 });
-router.get('/',function(req, res, next){
-    if(req.originalUrl == /v1/){
+
+router.get('/', function(req, res, next) {
+    if (req.originalUrl == /v1/) {
         return res.send({
-            info:'please set params like this: https://api.ioliu.cn/v1?url=http[s]://YourWantProxyUrl.com'
+            info: 'Please Set URL Like This: https://api.ioliu.cn/v1?url=http[s]://YourWantProxyUrl.com'
         });
     }
-    var url = req.originalUrl.replace('/v1/?url=','');
+    var url = req.originalUrl.replace('/v1/?url=', '');
+    url = url.indexOf('?') === -1 ? url.replace('&', '?') : url;
+    console.log('ref:' + req.get('reference'));
+    console.log('path:' + req.path);
+    console.log(req.subdomains);
     console.log(url);
-    getJSON(url,function(data){
-        return res.send(data);
-    })
-    //return res.send({url:url});
+    getJSON(url, function(data) {
+        if (req.query.callback) {
+            return res.jsonp(data)
+        } else {
+            return res.json(data)
+        }
+    });
 });
 
-function getJSON(url,callback,next){
-    request(url,function(err,res,body){
-        if(!err && res.statusCode == 200){
-            callback&&callback(body);
-        }else{
+function getJSON(url, callback, next) {
+    request(url, function(err, res, body) {
+        if (!err && res.statusCode == 200) {
+            callback && callback(body);
+        } else {
             console.log(err);
         }
     });
 }
 
-
-module.exports = router;
+module.exports = router;

+ 3 - 0
utils/utils.js

@@ -0,0 +1,3 @@
+export default{
+    
+}