无法将响应发送回节点js中的客户端

时间:2018-06-27 15:44:11

标签: node.js cors busboy

我已经编写了用于在节点js中使用busboy上传文件的代码。它执行成功。

但是,如果发生故障[任何该死的错误,包括未捕获的异常],我想捕获所有错误并将响应返回为静态500和“出了点问题” 。我使用域函数所做的。

问题1 :捕获到domain.on('error')事件中的错误后,即使编写并结束了响应。客户端应用程序仍然显示ajax响应挂起。请检查下图。

enter image description here

当我检查控制台错误时,我发现是这样的。

enter image description here

所以我认为这是由于跨源请求。因此,我使用app.use('cors')应用了npm的cors模块,控制台错误消失了。但是domain现在可以正常工作了。就像它没有得到错误。请检查下面的图像。

下面是我的Eclipse控制台,未应用cors请求 enter image description here

现在,在应用cors模块后,它变为

enter image description here

请在下面找到我的全部功能。

app.post('/fileupload', function(req, res) {
var fileUploadErrorDom = domain.create()
fileUploadErrorDom.run(function(){
fileUploadErrorDom.add(req);
fileUploadErrorDom.add(res);

    var fstream;
    var fileName, bunchId, standard, userKey, subject, unit, chapter, topic;
    req.pipe(req.busboy,function(err) {
        if(err) {
            logger.info('Error while piping request to write stream : '+err);
            throw err;
        }
    });

    req.busboy.on('file', function (fieldname, file, filename) {
        throw new Error('adsad')
        var fileExtension = validateFileAndReturnExtension(filename)
        if (fileExtension != null && fileExtension == 'PDF') {
            fstream = fs.createWriteStream(config.content_upload_path.pdf_path + filename );
        }
        else if (fileExtension != null && fileExtension == 'VIDEO') {
            fstream = fs.createWriteStream(config.content_upload_path.video_path + filename );
        }

        file.pipe(fstream,function(err) {
            if(err) {
                logger.info('Error while piping the file to write streams : '+err);
                throw err;
            }
        });

        fstream.on('close', function () {
            fileName = filename;
            fileObject = file;
            var paramData = {
                 topic : topic,
                 file : fileName,
                 userKey : userKey,
                 bunchId : bunchId
             };
            uploadFile(paramData, res)
        });

        fstream.on('error', function (err) {
            logger.info(err)
            generateWebServiceResponse(res, false, null, 200, err)
            res.send()
        });
    });

    req.busboy.on('field', function(fieldname, val) {
        if(fieldname == 'bunchId')
            bunchId = val;
        if(fieldname == 'userKey')
            userKey = val;
        if(fieldname == 'file')
            fileName = val;
        if(fieldname == 'standard')
            standard = val;
        if(fieldname == 'subject')
            subject = val;
        if(fieldname == 'unit')
            unit = val;
        if(fieldname == 'chapter')
            chapter = val;
        if(fieldname == 'topic')
            topic = val;
    });

    req.busboy.on('error', function(err) {
        logger.info(err)
        logger.info('console')
        throw err;
    });

})
fileUploadErrorDom.on('error', function(err) {
    logger.info('Domain error called !')
    logger.info('domain intercepted an error : '+err)
    res.writeHead(500, { 'Content-Type': 'application/json' });
    res.write(JSON.stringify({ success : false, returnMessage : "Something went wrong. Please contact administrator !" }));
    res.end();
//generateWebServiceResponse(res, false, null, 200, "Something went wrong. Please contact administrator !")
//res.redirect('back')
//res.end()
});
});

请帮助我。预先感谢。

0 个答案:

没有答案
相关问题