ExpressJS设置JSON解析的深度

ExpressJS设置JSON解析的深度,json,express,body-parser,Json,Express,Body Parser,我想在Express中间件Express.JSON()中设置JSON解析的深度 例如,如果我将选项设置为解析depth=1,那么 '{ "email": { "$ne": "user@example.com" } }' '{ "email": { "$ne": "user@example.com" } }' 将被解析为 { email: "[object Object]" } { email: { '$ne': 'user@example.com' } } --或-- 当我设置depth

我想在Express中间件
Express.JSON()
中设置JSON解析的深度

例如,如果我将选项设置为解析
depth=1
,那么

'{ "email": { "$ne": "user@example.com" } }'
'{ "email": { "$ne": "user@example.com" } }'
将被解析为

{ email: "[object Object]" }
{ email: { '$ne': 'user@example.com' } }
--或--

当我设置
depth=2

'{ "email": { "$ne": "user@example.com" } }'
'{ "email": { "$ne": "user@example.com" } }'
将被解析为

{ email: "[object Object]" }
{ email: { '$ne': 'user@example.com' } }
等等,

在这种情况下,不存在默认深度的问题,因为开发人员将知道在开发过程中允许多少嵌套


PS:它将防止应用程序受到NoSQL注入的攻击。

我写下查询,最大深度为6-8。在查找中使用查找时

  const [result] = await Collection.aggregate([
    { $match:statusObj },
     {
         $project:{
             _id:1,
             name:1
             }
     },
     {
      $lookup:{
           from:"articles",
            let: { "cat_id":"$_id"},
            pipeline:[
             {
                $match:{
                     $expr:{
                  $and: [
                         { $eq: ["$category_id", "$$cat_id"] }, 
                         { $eq: ["$isDeleted", false] },
                         { $eq: ["$type", type] }
                         ]
                     }

                    } 
                 },
                  {
      $lookup:{
           from:"view_articles",
            let: { "article_id":"$_id"},
            pipeline:[
             {
                $match:{
                     $expr:{
                  $and: [
                         { $eq: ["$article_id", "$$article_id"] }, 
                         { $eq: ["$isDeleted", false] }
                         ]
                     }

                    } 
                 }
                 ],
                 as:"viewCount"
          }    
    },
    {
      $addFields:{
          noOfViewCount : { $size:"$viewCount"}
          }   
      }          ],
                 as:"articleCategoryData"
          }    
    },
     {
      $addFields: {
      postCount: {$size:"$articleCategoryData"   },
      tempsArray: { $map:
        {
           input: "$articleCategoryData",
           as: "tempData",
           in: { $add: "$$tempData.noOfViewCount" }
        }
     },
                 },
      },
      {
        $addFields: {
          viewCount:{ $sum:"$tempsArray" }
              },
        },
        {
          $project:{
            _id: 1,
            name: 1,
            postCount: 1,
            viewCount: 1
              }
      },
      {
        $facet: {
          count: [
            {
              $count: "total"
            }
          ],
          result: [{ $match: {} }, { $skip: skipRecord }, { $limit: limit }]
        }
      }
]);
可以将深度设置为10。如果您觉得JSON出了问题,那么增加它:)

只需编写您自己的:

const get_depth=(obj)=>{
设深度=0
用于(obj中的常量键){
if(对象的obj[键]实例){
depth=Math.max(获取深度(obj[key]),深度)
}
}
返回深度+1
}
常数深度限制=2
const limit_depth=功能(请求、恢复、下一步){
如果(获取深度(请求正文)>深度限制)抛出新错误(“可能的NoSQL注入”)
下一个()
}
应用程序使用(限制深度)
或者,如果您更喜欢
“[对象]”

let limit_depth=(obj,当前_depth,limit)=>{
用于(obj中的常量键){
if(对象的obj[键]实例){
如果(当前深度+1==限制){
obj[key]=“[object]”//或类似的东西
}
else限制深度(obj[键],当前深度+1,限制)
}
}
}
使用(函数(req,res,next){limit\u depth(req.body,0,depth\u limit);next()})

如果任何人不想更改
req.body
的值,可以从


这是MongoDB的一部分。问题是关于express js。或者在更深入的体解析器库中使用express。再次阅读这些问题。谢谢你,我收到了,而且它在有效载荷方面帮助了我。在有效负载中只接收特定深度的JSON。我可以使用它创建自己的中间件吗?
限制深度
,因为它是一个完整的中间件。