Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express optional parameters

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

So if I have the following code, how would I add the optional parameters to the route path (currently /skittles)?

app.get('/skittles', callback);
like image 920
LanguagesNamedAfterCofee Avatar asked Oct 21 '22 08:10

LanguagesNamedAfterCofee


1 Answers

You don't need to add them to the path. You'll 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));
});
like image 55
Daniel Avatar answered Oct 24 '22 09:10

Daniel