question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error - csurf with multer

See original GitHub issue

Hello, im using csrf module (csurf) but when im uploading…

Error: invalid csrf token
   at verifytoken (D:\nodejs\node_modules\csurf\index.js:234:13)
   at csrf (D:\nodejs\node_modules\csurf\index.js:94:7)
   at Layer.handle [as handle_request] (D:\nodejs\node_modules\express\lib\router\layer.js:76:5)
   at trim_prefix (D:\nodejs\node_modules\express\lib\router\index.js:263:13)
   at D:\nodejs\node_modules\express\lib\router\index.js:230:9
   at Function.proto.process_params (D:\nodejs\node_modules\express\lib\router\index.js:305:12)
   at D:\nodejs\node_modules\express\lib\router\index.js:221:12
   at Function.match_layer (D:\nodejs\node_modules\express\lib\router\index.js:288:3)
   at next (D:\nodejs\node_modules\express\lib\router\index.js:182:10)
   at methodOverride (D:\nodejs\node_modules\method-override\index.js:77:5)`

var product=config.upload;

 uri.route('/up').get(function (req,res,next) {
              res.render('upload',{
                csrf  : req.csrfToken()
              });
              res.end();
      }).post(product,function  (req,res,next) {
        console.log('IN POST (/upload)');
        console.log(req.body)
        var filesUploaded = 0;
        if ( Object.keys(req.files).length === 0 ) {
            console.log('no files uploaded');
        } else {
            console.log(req.files)

            var files = req.files.file1;
            if (!util.isArray(req.files.file1)) {
                files = [ req.files.file1 ];
            } 

            filesUploaded = files.length;
        }
        res.json({ message: 'Finished! Uploaded ' + filesUploaded + ' files.  Route is /upload' });
      });

🔨

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
alvarotrigocommented, Oct 31, 2016

Fine, found the solution:

upload = multer({ 
    dest: 'uploads/' 
});

app.use(upload.single('demo'));
1reaction
LinusUcommented, May 1, 2015

Make sure that multer is added before the csurf middleware, like so:

app.use(multer())
app.use(csurf())

As far as I can tell it really should work then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSRF and Multer - Invalid CSRF Token Error - Stack Overflow
I have run into an issue with my multipart form when I added support for CSRF tokens to keep submissions secure. I have...
Read more >
Upload Single or Multiple Files with CSRF Protection - YouTube
Welcome to another video in this Node JS, Express Series and in this video we are going to talk about multer. A library...
Read more >
[Solved]-Graphql Invalid CSRF Token-node.js - appsloveworld
Csurf invalid csrf token Express / nodejs · Node Express and csurf - 403 ... CSRF and Multer - Invalid CSRF Token Error...
Read more >
cannot read properties of undefined (reading 'path') multer ...
I have encountered the same problem. But then I change the multer version back to 0.1.8 . Everything works fine. You may need...
Read more >
Express csurf middleware - Express 中文文档
Note: This page was generated from the csurf README. ... npm install csurf ... When the CSRF token validation fails, an error is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found