Answer the question
In order to leave comments, you need to log in
How to move the middleware chain from the route to the controller?
The problem grows from the fact that I don't fully understand how Express chains work.
The project has a route that properly processes post requests:
const express = require('express');
const router = express.Router();
const jsonParser = express.json();
const controller = require('../controllers/regController');
router.post('/', jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400);
console.log(req.body);
res.json(
{
serverErr: '',
fieldsErr: {
username: 'Тест',
}
}
);
})
module.exports = router;
router.post('/', jsonParser, controller.register);
module.exports.register = function (req, res) {
if (!req.body) return res.sendStatus(400);
console.log(req.body);
res.json(
{
serverErr: '',
fieldsErr: {
username: 'Тест',
}
}
);
})
Answer the question
In order to leave comments, you need to log in
const express = require('express');
const jsonParser = express.json();
module.exports.register = [jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400);
console.log(req.body);
res.json(
{
serverErr: '',
fieldsErr: {
username: 'Тест',
}
}
);
})]
const express = require('express');
const router = express.Router();
const controller = require('../controllers/regController');
router.post('/', controller.register)
module.exports = router;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question