Azure Node.js Easy API中间件

问题描述 投票:0回答:1

我有一个Azure node.js应用程序。我想将用于处理文件上传的“multer”中间件添加到一个POST easy-api中。

例如,我有文件./api/upload-files.js,它应该看起来像这样:

module.exports = {
    "post" : async (req, res, next) => {
        // ...
    }
};

我可以很容易地将multer中间件添加到./app.js文件中,其中Express应用程序已初始化:

const multer = require('multer');
app.post('*', multer({ storage: multer.memoryStorage() }).any());

但是,如果我不想将multer中间件添加到每个后端点,而只是添加到./api/upload-files.js中的那个,我该怎么做?

node.js azure express azure-mobile-services
1个回答
0
投票

原评论

这与您在应用程序中实现快速实例的方式有关。

如果您不希望将multer中间件用于某些请求,您可以在请求本身中使用您想要的函数,避免在将参数传递给请求方法时调用multer函数。

get端点的一个示例:

app.get('/getrequest', (req, res, next) => {
    console.log('Someone made a get request to your app');
})

后端点的一个示例:

app.post('/postrequest', (req, res, next) => {
    console.log('Someone made a POST request to your app');
})

请记住,以这种方式添加或删除中间件功能。

app.use('/user/:id', function (req, res, next) {
  console.log('I am a middleware!')
  next()
}, function (req, res, next) {
  console.log('I am another function')
  next()
})

Here are the docs

编辑

也许这段代码可以适应你的用例?

app.post('*', checkUseMulter);

function checkUseMulter(req, res, next) {
    if (['/mypathwithoutmulter', '/myotherpath'].includes(req.path)) {
        return next();
    }

    return multer({ storage: multer.memoryStorage() }).any();
}
© www.soinside.com 2019 - 2024. All rights reserved.