Advertisement
ADL_Rodrigo_Silva

Untitled

Jul 15th, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const User = require('../models/User');
  2.  
  3. const router = require('express').Router();
  4. const Joi = require('@hapi/joi');
  5.  
  6. const schemeRegister = Joi.object({
  7.     name: Joi.string().min(6).max(255).required(),
  8.     email: Joi.string().min(6).max(255).required().email(),
  9.     password: Joi.string().min(6).max(1024).required()
  10. });
  11.  
  12. router.post('/register', async (request, response) =>
  13. {
  14.     // Validación de la data que nos llega por el request
  15.  
  16.     const { error } = schemeRegister.validate(request.body);
  17.  
  18.     console.log("error", error);
  19.  
  20.     if (error) {
  21.         return response.status(400).json(
  22.             {
  23.                 error: error.details[0].message
  24.             }
  25.         )
  26.     }
  27.  
  28.     const user = new User(
  29.         {
  30.             name: request.body.name,
  31.             email: request.body.email,
  32.             password: request.body.password
  33.         });
  34.    
  35.     try {
  36.         const savedUser = await user.save();
  37.         response.json({
  38.             error: null,
  39.             data: savedUser
  40.         })
  41.     } catch (error) {
  42.         response.status(400).json({error});
  43.     }
  44.    
  45. });
  46.  
  47. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement