]> git.cworth.org Git - nodemuch/commitdiff
Get query string from server request.
authorRichard Worth <rdworth@gmail.com>
Sun, 5 Dec 2010 11:04:02 +0000 (03:04 -0800)
committerRichard Worth <rdworth@gmail.com>
Sun, 5 Dec 2010 11:04:02 +0000 (03:04 -0800)
Test url pathname for ending in /search, otherwirse return 404.

example.js

index e6a084bbe2381b0974b704b90da5603f35af8ad6..74e5701bd0c9137dcc33c63ada953e109e397b36 100644 (file)
@@ -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/" );