req.files始终为null express-fileupload [英] req.files is always null express-fileupload
问题描述
TLDR:express-jwt与在node.js API的req参数(例如,express-upload,例如multer)中添加内容的软件包产生冲突.
TLDR : express-jwt creates conflicts with packages adding stuff in the req parameter of the node.js API (express-upload, multer for example).
我正在使用node.js API,无法删除任何软件包.我正在尝试将文件从前端上传到API.我为此使用了许多软件包(express-fileupload,multer ...),但是它们都没有解决我的问题.创建API的人使用express-jwt进行身份验证.通过在API的req.user字段中添加一些信息来工作.
I'm working on a node.js API and I can't remove any package. I'm trying to upload files from the front end to the API. I used many packages for that (express-fileupload, multer ...) but none of them fixed my problem. The person who created the API used express-jwt for authentification. It works by adding some information in the req.user field on the API.
我的问题是,用于在nodejs中上传文件的软件包似乎与express-jwt产生冲突.如果我禁用它,则将按要求填充req.files,并且一切正常.但是,当我重新安装express-jwt时,req.files始终为null.我不能只使用另一个身份验证包.我不知道要解决这个问题.
My problem is that the packages used to upload files in nodejs seems to create conflicts with express-jwt. If i disable it, req.files is populated as it should and everything works perfectly. But when i reinstall express-jwt, req.files is always null. I can't just use another authentification package. I have no idea to solve this problem.
推荐答案
我知道了.您必须先调用express-fileupload中间件.我添加了以下代码,之后在任何调用的中间件中都可以访问req.files.
I figured it out. You have to call the express-fileupload middleware first. I added the following code and req.files was accessible in any middleware called after that.
const fileUpload = require('express-fileupload');
app.use(fileUpload());
app.use((req: any, res: any, next: any) => {
console.log("user : " + req.user + " ===== files : " + req.files);
next();
})
由于任何原因,如果您首先调用express-jwt,则不会填充req.files.对我而言,引起冲突的那条线就是这样:
For any reason if you call express-jwt first, req.files will not be populated. For me the line that caused the conflict was this one :
app.use(expressJwt({secret: PUBLIC, strict : false}).unless({path : unless}));
这篇关于req.files始终为null express-fileupload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!