index.js
765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;