代码之家  ›  专栏  ›  技术社区  ›  KARTHIKEYAN.A

如何解决feathersjs中请求实体过大的错误?

  •  4
  • KARTHIKEYAN.A  · 技术社区  · 7 年前

    我正在feathers服务中上传csv文件,文件大小为203kb,它会引发一个错误请求实体过大的错误,类似这样的错误。

    错误:

    {
        "name": "GeneralError",
        "message": "request entity too large",
        "code": 500,
        "className": "general-error",
        "data": {},
        "errors": {}
    }
    

    我正在编写类似这样的中间件代码,但我无法解决

    应用程序。js:

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    

    地方服务js:

    // Initializes the `location` service on path `/location`
    const createService = require('feathers-mongoose');
    const createModel = require('../../models/location.model');
    const hooks = require('./location.hooks');
    const filters = require('./location.filters');
    const multer = require('multer');
    const multipartMiddleware = multer();
    
    module.exports = function () {
      const app = this;
      const Model = createModel(app);
      const paginate = app.get('paginate');
    
      const options = {
        name: 'location',
        Model,
        paginate
      };
    
      // Initialize our service with any options it requires
      app.use('/location',
          multipartMiddleware.single('uri'),
            (req, res, next) => {
                req.feathers.file = req.file;
                next();
            },
        createService(options)
      );
    
      // Get our initialized service so that we can register hooks and filters
      const service = app.service('location');
    
      service.hooks(hooks);
    
      if (service.filter) {
        service.filter(filters);
      }
    };
    

    地方挂钩。js:

    const { authenticate } = require('feathers-authentication').hooks;
    
    module.exports = {
      before: {
        all: [ authenticate('jwt') ],
        find: [],
        get: [],
        create: [function(hook, next){
    
            var tHeader = Object.keys(hook.data)[0].split('\n')[0].replace(/\r?\n|\r/g, " ").split(',');
            var s = Object.keys(hook.data)[0].split('\n');
                s.shift();
            var tBody = s.map(d=> d.replace(/\r?\n|\r/g, " ").split(','));
            var jointObj = [];
    
            const mergeArrToJSON = (a, b) => a
                .map((item, i) => ({[item.replace(/\s+/g,'')]: b[i]}))
                .reduce((json,val)=>Object.assign({},json,val));
    
            tBody.forEach(p1=>{
                var a = tHeader, b = p1;
                jointObj.push(mergeArrToJSON(a,b))
            })
    
            var insertObj = [];
            jointObj.pop()
            jointObj.forEach(d=>{
                var Obj = {};
                for(k in d) {
                    var f = /[/]/;
                    if(f.test(k) == true){
                        var s = k.split('/');
                        if(Obj.hasOwnProperty(s[0]) !== true){
                            Obj[s[0]] = {[s[1].charAt(0).toLowerCase() + s[1].slice(1)]:d[k]};
                        } else {
                            Obj[s[0]][s[1].replace(/\s+/g,'')] = d[k].replace(/\s+/g,'');
                        }
                    } else {
                        Obj[k] = d[k];
                    }
                }
                insertObj.push(Obj)
            });
    
            hook.data = insertObj;
            next();
        }],
        update: [],
        patch: [],
        remove: []
      },
    
      after: {
        all: [],
        find: [],
        get: [],
        create: [],
        update: [],
        patch: [],
        remove: []
      },
    
      error: {
        all: [],
        find: [],
        get: [],
        create: [],
        update: [],
        patch: [],
        remove: []
      }
    };
    

    我用邮递员上传csv文件

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  9
  •   KARTHIKEYAN.A    7 年前

    我在应用程序中有更改代码。js文件

    之前:

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    

    app.use(bodyParser.json({limit: '100mb'}));
    app.use(bodyParser.urlencoded({
      limit: '100mb',
      extended: true
    }));
    
        2
  •  4
  •   Jasdev Singh Moun    5 年前

    打开应用程序。js文件并替换以下行

    app.use(express.json());
    app.use(express.urlencoded({ extended: true }));
    

    app.use(express.json({ limit: "50mb" }));
    app.use(express.urlencoded({ limit: "50mb", extended: true }));
    
        3
  •  2
  •   Emad Dehnavi    7 年前

    我在我的Node/express应用程序中也遇到了同样的问题,我使用了这个,它为我解决了这个问题,希望它也适用于您:

    app.use(bodyParser.json({limit:1024102420, type:'application/json'}));
    

    我使用数字格式作为限制,指定字节数