Guest User

Untitled

a guest
Aug 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. const joi = require("joi");
  2.  
  3. const createUserSchema = joi.object().keys({
  4. username: joi.string().alphanum().min(4).max(30).required(),
  5. password: joi.string().alphanum().min(2).max(30).required(),
  6. });
  7.  
  8. //Here begins my promise chain
  9. app.post("/createUser", (req, res) => {
  10. //validate javascript object against the createUserSchema before storing in database
  11. createUserSchema.validate(req.body)
  12. .catch(validationError => {
  13. res.sendStatus(400);
  14.  
  15. //CLEANLY ABORT the promise chain here
  16.  
  17. })
  18. .then(validatedUser => {
  19. //accepts a hash of inputs and stores it in a database
  20. return createUser({
  21. username: validatedUser.username,
  22. password: validatedUser.password
  23. })
  24. .catch(error => {
  25. res.sendStatus(500);
  26.  
  27. //CLEANLY ABORT the promise chain here
  28.  
  29. })
  30. //Only now after and if both promises are resolved do I send status 200
  31. .then(() => {
  32. res.sendStatus(200);
  33. }
  34. )
  35.  
  36. });
Add Comment
Please, Sign In to add comment