Last active
December 19, 2018 17:17
-
-
Save anshulrgoyal/dcaefdccaae6a1e3ba81c3cfde7a768e to your computer and use it in GitHub Desktop.
Handler Example
This file contains hidden or 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
| const handler = { | |
| // if path is localhost:3000/user | |
| user: (data, cb) => { | |
| const { | |
| method | |
| } = data; | |
| const allowedMethods = ["post", "get"]; | |
| if (allowedMethods.indexOf(method) !== -1) { | |
| handler._count[method](data, cb); | |
| } else { | |
| cb( | |
| 405, { | |
| err: "unknown method" | |
| }, | |
| "json" | |
| ); | |
| } | |
| }, | |
| _count: { | |
| // if method is post | |
| post: (data, cb) => { | |
| // do something | |
| }, | |
| get: (data, cb) => { | |
| // do something more | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment