Сначала необходимо создать Middleware
, получить и связать список сбора с запросом следующим образом.
const mongoose = require('mongoose');
const connection = mongoose.connect('mongodb://localhost:27017');
const getmethod = req.method;
app.use(function(req, res, next) {
connection.on('open', function () {
connection.db.listCollections().toArray(function (err, names) {
if (err) {
console.log(err);
//Error in get collection
req.collectionName = '';
mongoose.connection.close();
next();
} else {
console.log(names);
if(names.includes(getmethod)){
req.collectionName = getmethod;
}else{
//there is no collection exist for this method
req.collectionName = '';
}
mongoose.connection.close();
next();
}
});
});
});