From 260c11af146b497c59b9fa74dc7e29bd664de1eb Mon Sep 17 00:00:00 2001 From: Richard Worth Date: Sun, 5 Dec 2010 03:04:02 -0800 Subject: [PATCH] Get query string from server request. Test url pathname for ending in /search, otherwirse return 404. --- example.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/example.js b/example.js index e6a084b..74e5701 100644 --- a/example.js +++ b/example.js @@ -1,12 +1,18 @@ var http = require( "http" ), - exec = require( "child_process" ).exec, - notmuch; + parse = require( "url" ).parse, + exec = require( "child_process" ).exec; http.createServer( function(req, res) { - var q = "wiki"; - notmuch = exec( "notmuch search " + q, function( error, stdout, stderr ) { - res.writeHead( 200, { "Content-Type": "text/plain" } ); - res.end( stdout ); - }); -} ).listen( 8124, "127.0.0.1" ); + var url = parse( req.url ); + if ( url.pathname.split( "/" ).splice(-1)[0] === "search" ) { + exec( "notmuch search " + url.query, function( error, stdout, stderr ) { + res.writeHead( 200, { "Content-Type": "text/plain" } ); + res.end( stdout ); + }); + } else { + res.writeHead( 404 ); + res.end("404"); + } +}).listen( 8124, "127.0.0.1" ); + console.log( "Server running at http://127.0.0.1:8124/" ); -- 2.43.0