在我的网络浏览器中发生了巨大的崩溃

时间:2012-07-13 04:45:51

标签: javascript node.js upload express formidable

我正在尝试上传大文件(~2 GB)。我从Express中删除了bodyParser,以阻止服务器崩溃在大文件上。崩溃错误也是间歇性的,因此我甚至无法在错误发生时选择,以及原因。

代码如下:

channel = req.params.channel
  models.channel.findOne name: channel, (err, show) ->
    if err then console.log err
    if show?
      form = formidable.IncomingForm()
      files = []
      fields = []
      form.uploadDir = __dirname + '/../public/videos/resources/'

      form.on 'field', (field, value) ->
        #console.log field + ' ' + value
        fields.push [field,value]

      form.on 'file', (field, file) ->
        #console.log file
        files.push [field, file]

      form.on 'end', ->
        for file in files
          filename = file[1].name.replace /(.*)\//, ''
          ext = file[1].name.replace /(.*)\./, ''
          filename = uuid("#{filename}" + Date.now()) + ".#{ext}"
          fs.renameSync file[1].path, form.uploadDir + filename
          v = new models.video
          v.channel_id = channel._id
          v.title = 'Episode'
          v.description = ''
          v.url = filename

          v.save (err,results) ->
            if err then console.log err
            res.send 200

      form.parse req
    else
      res.send 403

有时上传会起作用,有时它会弹出以下内容:

Error: parser error, 0 of 65536 bytes parsed
    at IncomingForm.write (/Users/brendan/github/node_modules/formidable/lib/incoming_form.js:145:17)
    at IncomingMessage.<anonymous> (/Users/brendan/github/node_modules/formidable/lib/incoming_form.js:95:12)
    at IncomingMessage.emit (events.js:67:17)
    at HTTPParser.parserOnBody [as onBody] (http.js:105:21)
    at Socket.ondata (http.js:1506:22)
    at TCP.onread (net.js:374:27)

是否有其他人遇到过这个问题,或者知道是什么导致了这个问题?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的错误,问题是由我的中间件中对数据库的异步调用引起的。在设置请求数据事件的任何处理程序之前发送了异步调用。这导致强大的数据丢失。

可能相关的链接:

https://github.com/felixge/node-formidable/issues/130

https://github.com/felixge/node-formidable/issues/34

修改

中间件

getCategories = (req, res, next) ->
  data.getCategories (err, results) ->
    res.locals.categories = results
    next()

查询

data.getCategories = (callback) ->
    pg.connect connString, (err, client) ->
        client.query "SELECT name, slug FROM categories", (err, result) ->
            callback err, result.rows