Created
September 30, 2019 10:37
-
-
Save dbl0null/7079e5b70257d351d5ccb60be4f74fdd to your computer and use it in GitHub Desktop.
micro example
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
/* 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