I am coding a javascript http web server node, for a CDN project, I am used to PHP's
if ($_GET = "hello"); echo "Yey!"
but have no idea how to do so, I am new to javascript.
I am coding a javascript http web server node, for a CDN project, I am used to PHP's
if ($_GET = "hello"); echo "Yey!"
but have no idea how to do so, I am new to javascript.
NodeJS
var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
Without NodeJS:
Use URI.js to get the query string. https://medialize.github.io/URI.js/
var uri = URI("urn:uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a?hello=world");
uri.protocol() == "urn";
uri.path() == "uuid:c5542ab6-3d96-403e-8e6b-b8bb52f48d9a";
uri.query() == "hello=world";
You can Try this functions:
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
However, synchronous requests are discouraged, so you might want to use this instead:
function httpGetAsync(theUrl, callback)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
Notes: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.