Skip to content

Instantly share code, notes, and snippets.

@jacksoncharles
Created September 7, 2013 17:55
Show Gist options
  • Save jacksoncharles/6477719 to your computer and use it in GitHub Desktop.
Save jacksoncharles/6477719 to your computer and use it in GitHub Desktop.
node.js : handling url parameters
// Include http module,
var http = require("http"),
// And url module, which is very helpful in parsing request parameters.
url = require("url");
// Create the server.
http.createServer(function (request, response) {
// Attach listener on end event.
request.on('end', function () {
// Parse the request for arguments and store them in _get variable.
// This function parses the url from request and returns object representation.
var _get = url.parse(request.url, true).query;
// Write headers to the response.
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// Send data and end response.
response.end('Here is your data: ' + _get['data']);
});
// Listen on the 8080 port.
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment