Skip to content

Commit

Permalink
swap node-fetch for axios, proxy passes cookies from server
Browse files Browse the repository at this point in the history
  • Loading branch information
bcruddy committed Jan 29, 2020
1 parent b92e6d3 commit 8f2ccba
Show file tree
Hide file tree
Showing 5 changed files with 2,006 additions and 1,078 deletions.
4 changes: 2 additions & 2 deletions flyswatter.config.js
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'
}
};
68 changes: 47 additions & 21 deletions lib/proxy.js
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;
}, {})
};
});
}
5 changes: 2 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ module.exports = config => new Promise((resolve, reject) => {

app.use(express.static(config.path));

let server;
if (config.https) {
keygen()
.then(credentials => {
server = https.createServer(credentials, app);
const server = https.createServer(credentials, app);

resolve(server);
})
.catch(err => {
reject(err);
});
} else {
server = http.createServer(app);
const server = http.createServer(app);
resolve(server);
}
});
Loading

0 comments on commit 8f2ccba

Please sign in to comment.