Middleware
/validate/user.validate.js
user.validate.js
module.exports.postCreate = function(req, res, next) { var errors = []; if (!req.body.name) { errors.push('Name is required.'); } if (errors.length) { res.render('users/create', { errors: errors }) return; } next(); }
/routes/user.route.js
user.route.js
const express = require('express') const router = express() var controller = require('../controller/user.controller'); var validate = require('../validate/user.validate'); // GET method route router.get('/', controller.index); // GET method route router.get('/create', controller.create); // GET by id router.get('/:id', controller.get); // POST create user // Chạy validate.postCreate trước rồi mới chạy tới comtroller.postCreate thông qua method next() router.post('/create', validate.postCreate, controller.postCreate); // GET method route router.get('/search/name', controller.search); module.exports = router;
Command: npm start
<< back | next >>
0 nhận xét:
Post a Comment