Created
July 9, 2018 13:52
-
-
Save smeijer/4438e854d3362bc060e29efd432aca34 to your computer and use it in GitHub Desktop.
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
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