Authored by 周奇琪

项目初始化

  1 +node_modules/
  1 +灰度接口代理
  2 +--------------
  3 +
  4 +灰度接口服务:
  5 +
  6 +1. service.api.yohobuy.com
  7 +2. api.open.yohobuy.com
  8 +
  9 +配置:
  10 +
  11 +```json
  12 +{
  13 + apis:[
  14 + {
  15 + route:"/service.api.yohobuy.com",//老服务接口域名
  16 + methods:['get','post']
  17 +
  18 + },
  19 + {
  20 + route:"/api.open.yohobuy.com",//老服务接口域名
  21 + methods:['get','post']
  22 + }
  23 + ],
  24 + whiteList:[
  25 + {
  26 + api:'/guang/api/v1/article/getArticleNotice', //替换老接口
  27 + map:'http://api.douban.com/v2/book/search' //替换新接口
  28 + },
  29 + {
  30 + api:'app.Shopping.count',//替换老接口
  31 + map:'http://127.0.0.1:3000/test' //替换新接口
  32 + }]
  33 +}
  34 +
  35 +```
  36 +
  37 +启动:
  38 +
  39 +```
  40 +npm install
  41 +npm start
  42 +```
  1 +module.exports = {
  2 + apis:[
  3 + {
  4 + route:"/service.api.yohobuy.com",
  5 + methods:['get','post'],
  6 +
  7 + },
  8 + {
  9 + route:"/api.open.yohobuy.com",
  10 + methods:['get','post']
  11 + }
  12 + ],
  13 + whiteList:[
  14 + {
  15 + api:'/guang/api/v1/article/getArticleNotice',
  16 + map:'http://api.douban.com/v2/book/search'
  17 + },
  18 + {
  19 + api:'app.Shopping.count',
  20 + map:'http://127.0.0.1:3000/test'
  21 + }]
  22 +}
  1 +var http = require('http'),
  2 + conf = require("./conf");
  3 +var request = require('request');
  4 +var express = require('express');
  5 +var app = express();
  6 +var bodyParser = require('body-parser'); //body序列化插件
  7 +
  8 +app.use(bodyParser.urlencoded({extended: false}));
  9 +
  10 +var SERVICE_URL = "/service.api.yohobuy.com";//新服务的URL
  11 +
  12 +app.get('/test',function(req,res,next){
  13 + res.send('hi~~~');
  14 +});
  15 +
  16 +/**
  17 + * 路由生成
  18 + */
  19 +function route() {
  20 +
  21 + for(var i=0;i<conf.apis.length;i++) {
  22 +
  23 + var api = conf.apis[i];
  24 +
  25 + for(var j=0;j<api.methods.length;j++){
  26 +
  27 + if(api.route === SERVICE_URL) {
  28 + api.route += '/*';
  29 + }
  30 + console.log('method: ['+api.methods[j]+'] route:'+api.route);
  31 +
  32 + app[api.methods[j]](api.route, function(req, res, next) {
  33 + var form = req.body?req.body:{};
  34 + var url = whiteProc('http:/'+req.url,form,req.query);
  35 +
  36 + console.log('call method:'+req.method+' url:'+url);
  37 + procRes(req.method,url,req.body,res);
  38 + });
  39 + }
  40 + }
  41 +}
  42 +
  43 +/**
  44 + * 白名单处理
  45 + * @param {[type]} url [description]
  46 + * @param {[type]} form [description]
  47 + * @param {[query]} query [description]
  48 + * @return {[type]} [description]
  49 + */
  50 +function whiteProc(url,form,query) {
  51 +
  52 + var list = conf.whiteList;
  53 + var method = query.method?query.method:form['method'];
  54 + method = method?method:'';
  55 +
  56 + for(var i=0;i<list.length;i++) {
  57 + if(url.indexOf(list[i].api)>-1||method === list[i].api) {
  58 +
  59 + console.log('match api:'+list[i].api+' map:'+ list[i].map);
  60 + return list[i].map;
  61 + }
  62 + }
  63 + console.log('no match url:'+url);
  64 + return url;
  65 +}
  66 +
  67 +/**
  68 + * 处理代理参数
  69 + * @param {string} method 方法
  70 + * @param {string} url 代理地址
  71 + * @param {Object} form 表单数据
  72 + * @param {Object} res 响应信息
  73 + * @return {void}
  74 + */
  75 +function procRes(method,url,form, ress) {
  76 + if(method === "GET") {
  77 + request(url,callback);
  78 + } else {
  79 + request.post({url:url, form: form},callback);
  80 + }
  81 +
  82 + function callback(err,res,body){
  83 + if (!err && res.statusCode == 200) {
  84 + ress.send(body);
  85 + }
  86 + else {
  87 + if(res) {
  88 + console.log('errorCode:'+res.statusCode);
  89 + }
  90 + console.log(err);
  91 + ress.send(body);
  92 + }
  93 + }
  94 +}
  95 +
  96 +console.log('app start!');
  97 +route();
  98 +app.listen(3000);
  1 +{
  2 + "name": "grayapi",
  3 + "version": "1.0.0",
  4 + "description": "api proxy",
  5 + "main": "index.js",
  6 + "scripts": {
  7 + "test": "echo \"Error: no test specified\" && exit 1"
  8 + },
  9 + "author": "",
  10 + "license": "ISC",
  11 + "dependencies": {
  12 + "body-parser": "^1.14.1",
  13 + "express": "^4.13.3",
  14 + "request": "^2.67.0"
  15 + }
  16 +}