Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump diff from 3.4.0 to 3.5.0 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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