title: NodeJS error with connect-busboy, "TypeError: Cannot call method 'on' of undefined"
body: <p>I have this piece of code:</p>
  
  <pre><code>router.route("/post")
          .get(function(req, res) {
              ...
          })
          .post(authReq, function(req, res) {
              ...
              // Get uploaded file
              var fstream
              req.pipe(req.busboy)
              req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
              ...
  
              var fileSuffix = mimetype.split("/")[1] // Get suffix, may not always be correct
              var tmpFileName = postCount + "." + fileSuffix
              var tmpFileURL = __dirname + "/tmp/" + tmpFileName
              // Start writing
              fstream = fs.createWriteStream(tmpFileURL)
              file.pipe(fstream)
              fstream.on('close', function() {
                   ...
              })
          })
      })
  })
  </code></pre>
  
  <p>This use to work perfectly. However, I don't know quite what happened, but I picked up my computer today and found this error occurring every time I attempt to post:</p>
  
  <pre><code>TypeError: Cannot call method 'on' of undefined
      at IncomingMessage.Readable.pipe (_stream_readable.js:494:8)
      at /Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:139:8
      at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)
      at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13)
      at authReq (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/apiRoutes.js:17:3)
      at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)
      at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:100:13)
      at next (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:94:14)
      at Route.dispatch (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/route.js:81:3)
      at Layer.handle [as handle_request] (/Users/nathan/Library/Mobile Documents/com~apple~CloudDocs/Dev/LaughItUp/Code/Server/node_modules/express/lib/router/layer.js:82:5)
  
  _stream_readable.js:501
      dest.end();
           ^
  TypeError: Cannot call method 'end' of undefined
      at IncomingMessage.onend (_stream_readable.js:501:10)
      at IncomingMessage.g (events.js:180:16)
      at IncomingMessage.emit (events.js:92:17)
      at _stream_readable.js:943:16
      at process._tickCallback (node.js:419:13)
  </code></pre>
  
  <p>In my <code>index.js</code> file, I have this code to use Busboy:</p>
  
  <pre><code>app.use(busboy())
  </code></pre>
  
  <p>Then this code to include my route:</p>
  
  <pre><code>app.use("/api", require("./apiRoutes.js")())
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong> I was doing a bit more debugging and I think I pinpointed the issue. <code>req.busboy</code> is not defined. So I went digging through source code. Here is some of the code in the <code>connect-busboy</code> module:</p>
  
  <pre><code>if (req.busboy
  || req.method === 'GET'
  || req.method === 'HEAD'
  || !hasBody(req)
  || !RE_MIME.test(mime(req)))
  return next()
  </code></pre>
  
  <p>When I print out the actual values, busboy does not exist, the request method is 'POST', <code>hasBody(req)</code> results true, yet <code>RE_MIME.test(mime(req)))</code> results false, which is why busboy is not added to the request.</p>
  
  <p>My example file's MIME type is <code>image/gif</code>. Here is the regular expression <code>connect-busboy</code> is testing <code>image/gif</code> off of (aka <code>RE_MIME</code> variable):</p>
  
  <pre><code>/^(?:multipart\/.+)|(?:application\/x-www-form-urlencoded)$/i;
  </code></pre>
  
  <p>Therefore, I've come to the verdict I'm misunderstanding the API. Is Busboy only for HTML forms?</p>
  
  <p><strong>EDIT #2:</strong> I simply switched to <a href="https://github.com/expressjs/multer" rel="nofollow">Multer</a> and it works fine. However, I believe my issue was that I needed to place the file in a multipart part of the request; I'm not sure why it <em>was</em> working before. Thanks!</p>