0
I’m having two difficulties which are:
- 1) I’m trying to export and use mine middleware to be used locally.
- 2) I would also like to know if it is possible to use a middleware for a specific request type using the
Express Route
.
What is occurring in the first difficulty?
I’m only getting to use the middleware exported if it is global
, as local on the routes it seems that it is not called.
What is occurring in the second difficulty?
Any kind of requisition that’s nested that route will call that middleware, but if I want to put the middleware for a specific route which is the case of checkProjectPost
, how it should be done using the express router
?
Index.js
const express = require('express');
const cors = require('cors');
const bodyParser = require ('body-parser');
const routes = require('./routes');
const middleware = require('./middlewares');
const server = express();
server.use(cors());
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
// server.use(middleware.checkId);
server.use(routes);
server.listen(3000, 'localhost', () => console.log('SERVER [ONLINE]'));
module.exports = server;
Routes.js
const express = require('express')
const router = express.Router();
const middleware = require('./middlewares');
router.route('/projects', middleware.checkProjectPost)
.get((req, res) => {
console.log("GET")
return res.status(200).send();
})
.post((req, res) => {
req.body.tasks = [];
return res.status(200).send({ msg: 'Success, project created!' });
});
router.route('/projects/:id', middleware.checkId)
.put((req, res) => {})
.delete((req, res) => {});
router.route('/projects/:id/tasks', middleware.checkId)
.post((req, res) => {});
module.exports = router;
Middlewares.js
module.exports = {
checkId: (req, res, next) => {
const { id } = req.params;
return (!id && typeof id !== 'string')
? res.status(400).send({ errorMsg: 'The id property is empty or the type is different from string' })
: next();
},
checkProjectPost: (req, res, next) => {
console.log()
const { id, title } = req.body;
console.log (req)
if (!id && typeof id !== 'string' || !title && typeof title !== 'string') {
return res.status(400).send({ errorMsg: 'The id or title property is empty or the type is different from string' })
} else {
req.body.tasks = [];
return next();
}
},
showQuantityRequisition: (req, res, next) => {
}
}
Hello friend, thank you for the patience and willingness to answer, but your answer does not match what I expected, the user @user140828 answered the same perfectly. I would like to leave here also some code standardization tips for you, for example: You did req.body.password == null and also req.body.password == Undefined, for you to check if this property exists do not need to compare this way, just enter "req.body.password" will be checked if this property exists. And the second tip is about your code, separate your project by good practice.
– user148754
thank you very much Thiago I will refactor my codes this way from now on, thanks for the tip
– Teuuz1994