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

后端 未结 8 1116
感情败类
感情败类 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:37

    Express has a tool to check if your path will match the route you are creating : Express.js route tester.

    As Jose Mato says you have to decide how to structure your url:

    1. ?foo=1&foo=2
    2. ?foo[]=1&foo[]=2

    The http request should look like this, if you chose method 1:

    http://baseurl/api/?foo=1&foo=2

    Your route should have this logic:

    app.get('/api/:person', (req, res) => {
        /*This will create an object that you can iterate over.*/
        for (let foo of req.params.foo) {
          /*Do some logic here*/
        }
    });
    

提交回复
热议问题