出于某些原因,我需要能夠動態控制 routes express 使用(即,我需要能夠根據設置為活動狀態和不活動狀態動態啟用和禁用 URL/路徑)。這就是為什么 express 只添加了 1 個路徑:this._express.all('*', (req, res, next) => { console.log(req.path); console.log(req.method) if (req.path in this._routes){ this._routes[req.path](req, res, next); } else { res.sendStatus(404); } });但是,我有一個向服務器發送文件的上傳表單,使用 multer 的正常方法如下:let upload = multer({ dest: 'uploads/' })express.post('/upload', upload.single('file'), (req, res, next) => { // Do stuff and have access to req.file});但是,使用我當前的方法,我無法將 multer 中間件添加到動態路由中,并且我嘗試在調用時在我的自定義路由代碼中調用中間件/upload,但是,這無濟于事。有沒有辦法避免必須硬傳遞我的路線來表達并仍然使用中間件,例如 multer(以及可能通過路線添加的其他人)而不是express.use()?正如我之前所說,所有這些路由都可以通過管理面板啟用/禁用/修改,我寧愿不必添加實際的路由來表達并讓我自己的中間件處理請求的路由。
使用 multer 中間件表達動態路由
蕪湖不蕪
2023-03-18 17:42:48