Last change
on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | var _ = require('lodash')
|
---|
| 2 | var logger = require('./logger.js').getInstance()
|
---|
| 3 |
|
---|
| 4 | module.exports = {
|
---|
| 5 | getTarget: getTarget
|
---|
| 6 | }
|
---|
| 7 |
|
---|
| 8 | function getTarget(req, config) {
|
---|
| 9 | var newTarget
|
---|
| 10 | var router = config.router
|
---|
| 11 |
|
---|
| 12 | if (_.isPlainObject(router)) {
|
---|
| 13 | newTarget = getTargetFromProxyTable(req, router)
|
---|
| 14 | } else if (_.isFunction(router)) {
|
---|
| 15 | newTarget = router(req)
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | return newTarget
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | function getTargetFromProxyTable(req, table) {
|
---|
| 22 | var result
|
---|
| 23 | var host = req.headers.host
|
---|
| 24 | var path = req.url
|
---|
| 25 |
|
---|
| 26 | var hostAndPath = host + path
|
---|
| 27 |
|
---|
| 28 | _.forIn(table, function(value, key) {
|
---|
| 29 | if (containsPath(key)) {
|
---|
| 30 | if (hostAndPath.indexOf(key) > -1) {
|
---|
| 31 | // match 'localhost:3000/api'
|
---|
| 32 | result = table[key]
|
---|
| 33 | logger.debug('[HPM] Router table match: "%s"', key)
|
---|
| 34 | return false
|
---|
| 35 | }
|
---|
| 36 | } else {
|
---|
| 37 | if (key === host) {
|
---|
| 38 | // match 'localhost:3000'
|
---|
| 39 | result = table[key]
|
---|
| 40 | logger.debug('[HPM] Router table match: "%s"', host)
|
---|
| 41 | return false
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | })
|
---|
| 45 |
|
---|
| 46 | return result
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | function containsPath(v) {
|
---|
| 50 | return v.indexOf('/') > -1
|
---|
| 51 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.