-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swap node-fetch for axios, proxy passes cookies from server
- Loading branch information
Showing
5 changed files
with
2,006 additions
and
1,078 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module.exports = { | ||
https: true, | ||
port: 5050, | ||
port: 3000, | ||
path: './', | ||
proxy: { | ||
'/api': 'http://localhost:5050/api' | ||
'/api': 'http://localhost:3030/api' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,69 @@ | ||
const logger = require('./logger'), | ||
fetch = require('node-fetch'), | ||
bodyParser = require('body-parser'); | ||
const _ = require('lodash'), | ||
axios = require('axios'), | ||
bodyParser = require('body-parser'), | ||
logger = require('./logger'); | ||
|
||
module.exports = (config, app) => { | ||
if (!config.proxy) { | ||
return; | ||
} | ||
|
||
logger.info('configuring proxy...'); | ||
logger.info(`configuring proxy on ${config.port}`); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({extended: true})); | ||
|
||
Object.entries(config.proxy).forEach(([route, url]) => { | ||
Object.entries(config.proxy).forEach(([route, baseUrl]) => { | ||
logger.info(`set up proxy for ${route} -> ${baseUrl}`); | ||
app.use(route, (req, res) => { | ||
const endpoint = url + req.url; | ||
const fetchConfig = { | ||
headers: req.headers, | ||
method: req.method | ||
const { | ||
headers, | ||
method, | ||
body: data, | ||
query: params, | ||
url: pathname | ||
} = req; | ||
const reqConfig = { | ||
data, | ||
headers, | ||
method, | ||
params, | ||
url: baseUrl + pathname | ||
}; | ||
|
||
if (!isEmpty(req.body)) { | ||
fetchConfig.body = JSON.stringify(req.body); | ||
} | ||
axios(reqConfig) | ||
.then(({data, headers, status}) => { | ||
const cookies = parseAxiosCookies(headers['set-cookie']); | ||
|
||
fetch(endpoint, fetchConfig) | ||
.then(r => r.json()) | ||
.then(json => { | ||
res.json(json); | ||
cookies.forEach(cookie => { | ||
res.cookie(cookie.name, cookie.value, cookie.options); | ||
}); | ||
|
||
res.status(status).send(data).end(); | ||
}) | ||
.catch(err => { | ||
res.status(err.status || 500).send(err && err.message || '[flyswatter] unknown error'); | ||
res.status(err.status || 500).send('failed').end(); | ||
logger.info(err); | ||
}); | ||
}); | ||
}); | ||
|
||
logger.info('proxy configured successfully'); | ||
}; | ||
|
||
function isEmpty (obj) { | ||
return !obj || Object.keys(obj).length === 0; | ||
function parseAxiosCookies (setCookie) { | ||
return setCookie.map((cookie) => { | ||
const [kv, ...config] = cookie.split(';'); | ||
const [name, value] = kv.split('='); | ||
|
||
return { | ||
name, | ||
value, | ||
options: config.reduce((map, option) => { | ||
const [key, value] = option.trim().split('='); | ||
|
||
map[_.camelCase(key)] = value || true; | ||
|
||
return map; | ||
}, {}) | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.