Skip to content

Instantly share code, notes, and snippets.

@dbl0null
Created September 30, 2019 10:37
Show Gist options
  • Save dbl0null/7079e5b70257d351d5ccb60be4f74fdd to your computer and use it in GitHub Desktop.
Save dbl0null/7079e5b70257d351d5ccb60be4f74fdd to your computer and use it in GitHub Desktop.
micro example
/* legacy code here */
async function handlerA(data) { /* ... */ }
async function handlerB(data) { /* ... */ }
/* micro.js related code goes here */
const micro = require('micro')
async function dispatch(message) {
try {
switch (message.method) {
case 'getSomeData':
return { status: true, await handlerA(message.data) }
case 'getSomeMoreData':
return { status: true, await handlerB(message.data) }
default:
return { status: false, message: 'Unknown method' }
}
} catch (error) {
throw error;
}
}
if (require.main === module) {
micro(async req => JSON.stringify(await dispatch(await micro.json(req))))
.listen(process.env.PORT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment