我就是这样用穆特的,
const specialExample = multer({ .. blah blah })
app.post(
'/specialPath',
specialExample.single('handled_by_multer'),
await function (req, res) {
fs.renameSync(.. move the file, etc
// send a reply
res.set('Content-Type', 'text/plain')
res.status(200).send(`ok`)
})
所以请注意,事情按如下顺序发生:
app.post(
'/specialPath',
// EXPRESS KNOWS TO GO THIS WAY
specialExample.single('handled_by_multer'),
// MULTER LITERALLY IS GETTING THE FILE
// MULTER HAS COMPLETELY GOT AND SAVED THE FILE
await function (req, res) {
// THIS FUNCTION NOW BEGINS RUNNING
... etc
这一切都很好.
现在,想象一下,我想看看一些参数.
在某些情况下,我DO NOT想要得到文件.
const specialExample = multer({ .. blah blah })
app.post(
'/specialPath',
specialExample.single('handled_by_multer'),
await function (req, res) {
const meta = req.body.meta
if (meta !== "wth") {
.. we actually DON'T want the file
.. delete the file
.. send a 401 and return
}
fs.renameSync(.. move the file, etc
// send a reply
res.set('Content-Type', 'text/plain')
res.status(200).send(`ok`)
})
请注意,这可以很好地工作,但是,我们正在浪费地获取/保存文件(然后才删除它).
有没有办法做更多这样的事情......
app.post(
'/specialPath',
await function (req, res) {
.. if I happen to want to, only then: {
specialExample.single('handled_by_multer'),
}
})
怎么做?