I need to create url for get which is going to accept array, how in node.js/express extract array from request?

后端 未结 8 1101
感情败类
感情败类 2020-12-04 22:01

I need to create url for get which is going to accept array, how in node.js/express extract array from request ? I need to pass array with names which parametes I need to ba

相关标签:
8条回答
  • 2020-12-04 22:44

    If you want to pass an array from url parameters, you need to follow the bellow example:

    Url example:

    https://website.com/example?myarray[]=136129&myarray[]=137794&myarray[]=137792
    

    To retrieve it from express:

    console.log(req.query.myarray)
    [ '136129', '137794', '137792' ]
    
    0 讨论(0)
  • 2020-12-04 22:47

    Express exposes the query parameter as an array when it is repeated more than once in the request URL:

    app.get('/', function(req, res, next) {
       console.log(req.query.a)
       res.send(200)
    }
    
    GET /?a=x&a=y&a=z:
    // query.a is ['x', 'y', 'z']
    

    Same applies for req.body in other methods.

    0 讨论(0)
提交回复
热议问题