Skip to content

Instantly share code, notes, and snippets.

@smeijer
Created July 9, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeijer/4438e854d3362bc060e29efd432aca34 to your computer and use it in GitHub Desktop.
Save smeijer/4438e854d3362bc060e29efd432aca34 to your computer and use it in GitHub Desktop.
import bodyParser from 'body-parser';
import url from 'url';
const handleRequest = (method, path, cb) => {
WebApp.rawConnectHandlers.use(path, (req, res, next) => {
if (req.method !== method) {
next();
return;
}
const queryString = url.parse(req.url).query || '';
const queryParams = { query: {} };
queryString.split('&').forEach(pair => {
queryParams.query[pair.split('=')[0]] = pair.split('=')[1];
});
Promise.resolve()
.then(
() =>
new Promise(resolve => {
bodyParser.urlencoded({ extended: false })(req, res, resolve);
}),
)
.then(
() =>
new Promise(resolve => {
bodyParser.json({ limit: '30mb' }).call(null, req, res, resolve);
}),
)
.then(() => cb(queryParams, req, res, next))
.catch(e => {
console.error('Exception unhandled:', e.stack);
next();
});
});
};
export default handleRequest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment