我正在使用:nodejs ..
我的问题: 我正在尝试上传图像,但是一旦我将表单enctype更改为multipart / form-data,csurf程序包就会引发此错误:
ForbiddenError:无效的CSRF令牌 在csrf(D:\ projects \ node \ Real Life Shop Project \ node_modules \ csurf \ index.js:112:19) 在Layer.handle上[作为handle_request](D:\ projects \ node \ Real Life Shop Project \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(D:\ projects \ node \ Real Life Shop Project \ node_modules \ express \ lib \ router \ index.js:317:13) 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(D:\ projects \ node \ Real Life Shop Project \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(D:\ projects \ node \ Real Life Shop Project \ node_modules \ express \ lib \ router \ index.js:275:10) 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ express-session \ index.js:495:7 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ connect-mongodb-session \ index.js:124:20 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:410:17 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:398:11 在ClientSession.endSession上(D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb-core \ lib \ sessions.js:134:41) 在executeCallback(D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:395:17) 在handleCallback(D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:128:55) 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ operations \ collection_ops.js:598:5 在D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:410:17 在executeCallback(D:\ projects \ node \ Real Life Shop Project \ node_modules \ mongodb \ lib \ utils.js:402:9)
Csurf程序包与enctype =“ application / x-www-form-urlencoded”表单配合使用很好,
以下是我的代码,用于csurf配置:
const mongodbUri = 'mongodb://localhost:27017/shop';
// Configuring Session
const store = new MONGODB_CONNECT({
uri: mongodbUri,
dbName: 'shop',
collection: 'users-session'
});
app.use(session({
secret: "Some secret text",
resave: false,
saveUninitialized: false,
store
}));
app.use(csrf);
app.use((req, res, next) => {
res.locals.loggedIn = req.session.isLoggedIn;
res.locals.user = req.session.user;
res.locals.csrfToken = req.csrfToken();
return next();
});
注意:我没有包括require语句,因为那只是我的问题中的多余声音,
请帮助我摆脱这个问题