index.js 765 Bytes
var express = require('express');
var router = express.Router();
var apiReg = /^\/api\//

const Api = require('./api')

const API_MAP = {
  label: {
    create: '/api',
    list: '/yohobuy-platform-web/snsMark/list'
  }
}

const ENDPOINT = 'http://172.16.1.201:8081/'

router.use('/', function(req, res, next) {
  let api = new Api(ENDPOINT)

  console.log(req.path)

  if (!apiReg.test(req.path)) {
    return next({
      code: 400
    })
  }

  let path = req.path.toLowerCase()

  let [scope, method] = path.split(apiReg)[1].split('/')

  let url = API_MAP[scope][method]

  if (!url) {
    return next({
      code: 500
    })
  }

  api[req.method.toLowerCase()](url, req.body).then(data => {
    res.json(data)
  }).catch(next)
});

module.exports = router;