Thanks - I do show URL being used in the examples for a web server, but the current solution isn't great. I've just found that in node you can do url.parse(req.url,true) which should give you all the info you need (http://nodejs.org/docs/latest/api/url.html). While Espruino has this implemented, it doesn't parse the query string, which is what you need. I've created a bug for this and hopefully I'll get around to fixing it soon.
In the mean time, you can try something like this:
function parseURL(url) {
var idx = url.indexOf("?");
var map = {};
if (idx>=0) {
var args = url.substr(idx+1).split("&");
for (var n in args) {
var arg = args[n];
var eq = arg.indexOf("=");
if (eq<0) eg=arg.length;
map[arg.substr(0,eq)] = arg.substr(eq+1);
}
}
return map;
}
parseURL("/hello?key=val&foo=bar&something");
// ={"key":"val","foo":"bar","something":''}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi Sven,
Thanks - I do show URL being used in the examples for a web server, but the current solution isn't great. I've just found that in node you can do
url.parse(req.url,true)
which should give you all the info you need (http://nodejs.org/docs/latest/api/url.html). While Espruino has this implemented, it doesn't parse the query string, which is what you need. I've created a bug for this and hopefully I'll get around to fixing it soon.In the mean time, you can try something like this: