如何在Joi / Hapi中设置最大图像尺寸

Car*_*los 1 node.js hapijs joi

我正在使用nodejs + mongodb。在上传图片时,如果前端大小超过50kb,则不允许这样做。我想将其设置为最大5MB,以便可以在大小为1MB或2MB的情况下上传图像。我尝试了很多方法,但我仍然没有找到适当的解决方案。我检查了此链接[Joi / hapi] [1]

[1]:https//github.com/hapijs/joi/blob/v9.0.1/API.md,但仍然无法正常工作。有人可以帮我吗

这是我的图式

const nameofSchema = Joi.object().keys({
description: Joi.string(), // .required(),
image: Joi.string().max(500000),

category: Joi.string(),
namesasd: Joi.string().regex(/^[a-z][a-z0-9-]*$/),
title: Joi.string(), // .required(),
price: Joi.number().integer(),
tag: Joi.object().keys({
    tag_name: Joi.string()
})
});
Run Code Online (Sandbox Code Playgroud)

在此图像中,我想将最大限制大小设置为5mb。(上传最大5mb的图像)

以下是路线

create: {
    description: '',
    path: '/',
    verb: 'post'
},
Run Code Online (Sandbox Code Playgroud)

Cut*_*ert 8

您不会使用Joi设置文件大小限制。这是路由配置选项。您可以将config.payload.maxBytes属性设置为所需的字节数。默认值为1 Mb。小例子:

{
  method: 'POST',
  path: '/upload',
  config: {
    payload: {
      maxBytes: 1000 * 1000 * 5, // 5 Mb
      output: 'stream',
      parse: true
    },
    validate: {
      payload: {
        file: Joi.any()
      }
    }
  },
  handler: function(request, reply) {
    /* do stuff with your file */
  }
}
Run Code Online (Sandbox Code Playgroud)