最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How resolve request entity too large error in feathersjs? - Stack Overflow

programmeradmin3浏览0评论

I'm uploading csv file in feathers service and the file size is 203kb, it throw an error request entity too large error something like this.

error:

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

i'm writing middle ware code something like this but i can't able to resolve

app.js:

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

location.service.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);
  }
};

location.hooks.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: []
  }
};

i'm using postman to upload csv file

I'm uploading csv file in feathers service and the file size is 203kb, it throw an error request entity too large error something like this.

error:

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

i'm writing middle ware code something like this but i can't able to resolve

app.js:

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

location.service.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);
  }
};

location.hooks.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: []
  }
};

i'm using postman to upload csv file

Share Improve this question edited Nov 23, 2017 at 11:41 KARTHIKEYAN.A asked Nov 23, 2017 at 7:19 KARTHIKEYAN.AKARTHIKEYAN.A 20.1k10 gold badges136 silver badges149 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11

I have change code in app.js file

before:

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

after:

app.use(bodyParser.json({limit: '100mb'}));
app.use(bodyParser.urlencoded({
  limit: '100mb',
  extended: true
}));

open app.js file and replace following lines

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

With

app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));

I had the same issue in my Node/express app, I used this and it fixed the issue for me, Hope it works for you also :

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

I used the number format for limit which specifies the number of bytes

发布评论

评论列表(0)

  1. 暂无评论