Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const joi = require("joi");
- const createUserSchema = joi.object().keys({
- username: joi.string().alphanum().min(4).max(30).required(),
- password: joi.string().alphanum().min(2).max(30).required(),
- });
- //Here begins my promise chain
- app.post("/createUser", (req, res) => {
- //validate javascript object against the createUserSchema before storing in database
- createUserSchema.validate(req.body)
- .catch(validationError => {
- res.sendStatus(400);
- //CLEANLY ABORT the promise chain here
- })
- .then(validatedUser => {
- //accepts a hash of inputs and stores it in a database
- return createUser({
- username: validatedUser.username,
- password: validatedUser.password
- })
- .catch(error => {
- res.sendStatus(500);
- //CLEANLY ABORT the promise chain here
- })
- //Only now after and if both promises are resolved do I send status 200
- .then(() => {
- res.sendStatus(200);
- }
- )
- });
Add Comment
Please, Sign In to add comment