Express middleware for validating a MongoDB ObjectId in the request path.
npm install valid-oid
Import the middleware as below:
const validId = require('valid-oid');
Install the middleware on a route:
app.get('/api/product/id/:productId', validId('productId'), (req, res) => {
const productId = req.productId;
const product = getProductById(productId);
if (!product) {
return res.status(404).json({ error: `Product "${productId}" not found.` });
} else {
return res.json(product);
}
});
-
The middleware looks for a path param with the provided name.
-
If the param exists and is valid, the validated ObjectId is added to the request, and control passes to the next middleware.
-
Otherwise, a 404 response is sent.