Skip to content

Instantly share code, notes, and snippets.

@kasramp

kasramp/index.js Secret

Created May 21, 2020 14:09
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 kasramp/20e369ef57322e1cd88df7de73c0e554 to your computer and use it in GitHub Desktop.
Save kasramp/20e369ef57322e1cd88df7de73c0e554 to your computer and use it in GitHub Desktop.
router.post("/v1/users", async (context) => {
if (!context.request.hasBody) {
context.response.status = 400;
context.response.body = { error: "Request body cannot be empty" };
} else {
const { firstName, lastName, age } = (await context.request.body(true)).value
const ageInt = parseInt(age);
if (firstName && lastName && ageInt) {
dummyUsers.push({ id: dummyUsers.length + 1, firstName: firstName, lastName: lastName, age: ageInt });
context.response.status = 201;
context.response.body = dummyUsers[dummyUsers.length - 1];
} else {
context.response.status = 400;
context.response.body = { error: "Invalid payload's provided" };
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment