|
| 1 | +const express = require("express"); |
| 2 | +const app = express(); |
| 3 | +const port = 4001; |
| 4 | +app.use(express.json()); |
| 5 | + |
| 6 | +const Joi = require("joi"); |
| 7 | +const schema = Joi.object({ |
| 8 | + first_name: Joi.string().alphanum().min(3).max(40).required(), |
| 9 | + last_name: Joi.string().alphanum().min(3).max(40).required(), |
| 10 | + email: Joi.string() |
| 11 | + .email({ |
| 12 | + minDomainSegments: 2, |
| 13 | + tlds: { allow: ["com", "net"] }, |
| 14 | + }) |
| 15 | + .required(), |
| 16 | + password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{8,30}$")), |
| 17 | + |
| 18 | + password_confirmation: Joi.ref("password"), |
| 19 | +}) |
| 20 | + .with("first_name", "last_name") |
| 21 | + .with("password", "password_confirmation"); |
| 22 | + |
| 23 | +var cmsRouter = express.Router(); |
| 24 | +cmsRouter.post("/api/user_validation", async (req, res, next) => { |
| 25 | + console.log("Input validation"); |
| 26 | + console.log(req.body); |
| 27 | + try { |
| 28 | + const { error, value } = schema.validate(req.body); |
| 29 | + console.log(error.toString()); |
| 30 | + res.status(200).json(error.toString()); |
| 31 | + } catch (err) { |
| 32 | + next(err); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +app.use(cmsRouter); |
| 37 | + |
| 38 | +app.use(function (err, req, res, next) { |
| 39 | + console.log("Calling actual error handler:"); |
| 40 | + res.status(200).json(err.toString()); |
| 41 | +}); |
| 42 | + |
| 43 | +app.listen(port, () => { |
| 44 | + console.log(`Example app listening on port ${port}`); |
| 45 | +}); |
0 commit comments