Express advanced options

I need a path like this: /skittles?type[]=blue&type[]=green (like x-www-form-urlencoded , but this is an api request).

So, if I have the following code, how would I add additional parameters to the route of the route (currently / pins)?

 app.get('/skittles', callback); 
+4
source share
2 answers

You do not need to add them to the path. You will find them in the req.query object.

 var util = require('util'); app.get('/skittles', function(req, res) { console.log(req.query); var type = req.query.type || []; console.log("type: "+util.inspect(type)); res.send("Type: "+util.inspect(type)); }); 
+4
source

you can confirm your input query parameters you received in req.body or req.query like this.

 app.post('/v1/api/test-api', function(req, res) { var parameters = []; if(req.body.userName !== undefined) { //DO SOMEHTING parameters.push({username: req.body.userName}); } if(req.body.userId !== undefined) { //DO SOMEHTING parameters.push({userId: req.body.userId}); } if(req.body.userEmail !== undefined) { //DO SOMEHTING parameters.push({userEmail: req.body.userEmail}); } res.json({receivedParameters: parameters}); }); 
0
source

Source: https://habr.com/ru/post/1483532/


All Articles