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

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

    You can pass array elements separated by slashes - GET /api/person/foo/bar/...

    Define your route as '/api/person/(:arr)*'

    req.params.arr will have the first parameter. req.params[0] will have the rest as string. You split and create an array with these two.

    app.get('/api/person/(:arr)*', function(req, res) {
            var params = [req.params.arr].concat(req.params[0].split('/').slice(1));
            ...
    });
    
    GET /api/person/foo
    params = ["foo"]
    
    GET /api/person/foo/bar
    params = ["foo", "bar"]
    

    ...

提交回复
热议问题