在node.js中发布请求“待定”

时间:2018-08-17 15:25:18

标签: node.js express async.js

我正在使用Node.js Express框架上传文件,使用async.js处理该文件,并通过对/ design的POST请求将该文件呈现在页面上。然后,用户可以操作该图像,并可以按照对/ cart的POST请求转到其购物车。通过/ design上传和处理文件的过程完全符合我的要求,但是一旦用户继续浏览/ cart,此请求将保持待处理状态,而不会产生任何结果。

详细信息:

用户通过POST请求将文件上传到/ design,然后进行处理:

router.post('/design', function(req, res) {

    var file;
    req.session.uuid = sessionHandler.createUuid();


  file = req.files.photo;

    tasks = [

        function fileUpload(cb){

            fileHandler.upload(req.session,file,method,function(fileProperties){
                for(var k in fileProperties) req.session[k]=fileProperties[k];
                cb();
            });

        },
        function setCookies(cb){

            var imgData = {
                S3FilePath: req.session.S3FilePath,
                fullFilePath: req.session.fullFilePath,
                uuid: req.session.uuid,
                S3PreviewPath: req.session.S3PreviewFilePath
            };

            res.cookie('imgData', JSON.stringify(imgData), { maxAge: 900000, httpOnly: false, encode: String});

            cb();

        }
    ];

    async.series(tasks, (err, results) => {
        if (err){
            console.log('Error:' + err);
        } else{
            console.log('Rendering image');
            res.render('app',{ title : 'Designer' });
            res.end();
        }
    });

});

/ design POST请求使用以下模块上传和处理文件。

var upload = function(session,file,callback){

  var fileProperties = {};

  tasks = [
      function createDir(cb){
            mkdirp('./public/uploads/'+session.uuid+'/', function(err) {
                if (!err) {
                    console.log('directory created');
                    return cb();
                } else{
                    cb(err);
                }
            });
        },
        function getFileExtension(cb){
            request.post({
                url:'http://localhost:1652/framer/getextension',
                form: {
                    filePath: file.path
                }
            }, function(err,httpResponse,body){
                if (err) console.log(err);
                fileProperties.extension = body;
                return cb();
            });
        },
        function uploadLocal(cb){

            fileProperties.fileName = session.uuid + '.' + fileProperties.extension;
            fileProperties.previewPath = '/uploads/' + session.uuid + '/' + session.uuid + '-preview.' + fileProperties.extension;
            fileProperties.fullFilePath = path.join(__dirname, '/../public/uploads/' + session.uuid + '/' + fileProperties.fileName);
            fs.rename(file.path, fileProperties.fullFilePath, function(err){
                if (err) throw cb(err);
                console.log('file uploaded locally');
                cb();
            });

        },
        function uploadS3(cb){
            request.post({
                url:'http://localhost:1656/s3/put',
                form: {
                    filePath: fileProperties.fullFilePath,
                    fileName: fileProperties.fileName
                }
            }, function(err,httpResponse,body){
                if (err) throw cb(err);
                console.log('file uploaded to S3');
                fileProperties.S3FilePath = 'https://s3-ap-southeast-2.amazonaws.com/bucket/designer/'+fileProperties.fileName;
                return cb();
            }); 
        },
        function generatePreview(cb){
        console.log("Calling preview method");
            request.post({
                url:'http://localhost:1652/framer/preview',
                form: {
                    filePath: fileProperties.fullFilePath,
                    extension: fileProperties.extension,
                    dirName: __dirname,
                    uuid: session.uuid
                }
            }, function(err,httpResponse,body){
                if (err) throw cb(err);
                fileProperties.previewFullFilePath = body;
                return cb();
            });
        },
        function uploadPreviewS3(cb){
            request.post({
                url:'http://localhost:1656/s3/put',
                form: {
                    filePath: fileProperties.previewFullFilePath,
                    fileName: session.uuid + '-preview.' + fileProperties.extension
                }
            }, function(err,httpResponse,body){
                if (err) throw cb(err);
                console.log('Preview file uploaded to S3');
                fileProperties.S3PreviewFilePath = 'https://s3-ap-southeast-2.amazonaws.com/bucket/designer/'+session.uuid + '-preview.' + fileProperties.extension;
                return cb();
            }); 
        }
  ];
  async.series(tasks, (err, results) => {
      if (err) {
        console.log('File upload error:' + err);
      } else{
        callback(fileProperties);
      }
  });

};

当想要继续下一页时,发帖请求会将用户带到他们的购物车,我现在将其简化为仅显示console.log消息。

router.post('/cart', function (req, res){

    console.log('Go to payment');
    res.end();

});

此请求开始后,Chrome开发者工具中的请求将继续保持“待处理”模式,此后再无任何反应。一段时间后,终端中将显示以下内容:

POST /cart - - ms - -

在上传过程中显示的其他错误是ERR_HTTP_HEADERS_SENT错误和MaxListenersExceededWarning错误。不幸的是,除非我没有以正确的方式使用async.js,否则我无法在代码中的任何地方找到发送标头的原因。其次,我不确定为什么会出现MaxListenersExceededWarning错误,因为它是单个POST请求。

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/response.js:170:12)
    at ServerResponse.send (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:124:29)
    at finalize (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:47:21)
    at toolbar_callback (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:99:17)
    at Object.exports.renderFile (/Users/dazzatron/designer/app/frontpage/node_modules/pug/lib/index.js:412:12)
    at View.exports.__express [as engine] (/Users/dazzatron/designer/app/frontpage/node_modules/pug/lib/index.js:455:11)
    at View.render (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/view.js:135:8)
directory created
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/response.js:767:10)
    at ServerResponse.send (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/response.js:170:12)
    at ServerResponse.send (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:124:29)
    at finalize (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:47:21)
    at toolbar_callback (/Users/dazzatron/designer/app/frontpage/node_modules/express-debug/lib/response.js:99:17)
    at Object.exports.renderFile (/Users/dazzatron/designer/app/frontpage/node_modules/pug/lib/index.js:412:12)
    at View.exports.__express [as engine] (/Users/dazzatron/designer/app/frontpage/node_modules/pug/lib/index.js:455:11)
    at View.render (/Users/dazzatron/designer/app/frontpage/node_modules/express/lib/view.js:135:8)
file uploaded locally
file uploaded to S3
Calling preview method
Preview file uploaded to S3
Rendering image
POST /design 200 4516.438 ms - 480447
(node:95893) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 field listeners added. Use emitter.setMaxListeners() to increase limit
(node:95893) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 file listeners added. Use emitter.setMaxListeners() to increase limit
(node:95893) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit
(node:95893) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit
POST /cart - - ms - -

任何有关我的POST请求为何一直卡住的帮助将不胜感激。

0 个答案:

没有答案