diff options
-rw-r--r-- | pnhandler.js | 59 | ||||
-rw-r--r-- | route.js | 8 | ||||
-rw-r--r-- | tinyserver.js | 21 |
3 files changed, 74 insertions, 14 deletions
diff --git a/pnhandler.js b/pnhandler.js index 4123049..dba03db 100644 --- a/pnhandler.js +++ b/pnhandler.js @@ -5,41 +5,90 @@ */ var util = require('util'); +var querystring = require('querystring'); -function noop(query, res) { +function noop(query, reqdata, res) { util.log('[pnhandler] handling ' + query.pathname + ', ok who the fuck is messing with us?'); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(); } -function register(query, res) { +function register(query, reqdata, res) { util.log('[pnhandler] handling ' + query.pathname); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(); } -function login(query, res) { +function login(query, reqdata, res) { util.log('[pnhandler] handling ' + query.pathname); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(); } -function poll(query, res) { +function poll(query, reqdata, res) { util.log('[pnhandler] handling ' + query.pathname); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(); } -function upload(query, res) { +function upload(query, reqdata, res) { util.log('[pnhandler] handling ' + query.pathname); + res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(); } +function hello(query, reqdata, res) { + util.log('[pnhandler] handling ' + query.pathname); + + var body = + '<!DOCTYPE html>\n' + + '<html>\n' + + '<head>\n' + + '<title>Hello</title>\n' + + '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n' + + '</head>\n' + + '<body>\n' + + '<form action="/hellores" method="post">\n' + + '<textarea name="text" rows="20" cols="60"></textarea><br />\n' + + '<input type="submit" value="Submit text" />\n' + + '</form>\n' + + '</body>\n' + + '</html>\n'; + + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(body); + res.end(); +} + +function hellores(query, reqdata, res) { + util.log('[pnhandler] handling ' + query.pathname); + util.log('[pnhandler] hello recv:\n' + querystring.parse(reqdata).text); + + var body = + '<!DOCTYPE html>\n' + + '<html>\n' + + '<head>\n' + + '<title>Hello Response</title>\n' + + '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n' + + '</head>\n' + + '<body>\n' + + '<p>You requested/sent:<br /></p>' + + querystring.parse(reqdata).text + + '</body>\n' + + '</html>\n'; + + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(body); + res.end(); +} + exports.noop = noop; exports.register = register; exports.login = login; exports.poll = poll; exports.upload = upload; +exports.hello = hello; +exports.hellores = hellores; @@ -14,12 +14,14 @@ var handle = { '/register': pnhandler.register, '/login': pnhandler.login, '/poll': pnhandler.poll, - '/upload': pnhandler.upload + '/upload': pnhandler.upload, + '/hello': pnhandler.hello, + '/hellores': pnhandler.hellores }; -function route(query, res) { +function route(query, reqdata, res) { if (typeof handle[query.pathname] === 'function' ) { - handle[query.pathname](query, res); + handle[query.pathname](query, reqdata, res); } else { util.log('[route] no handler found for ' + query.pathname); diff --git a/tinyserver.js b/tinyserver.js index fd9d37d..ac2560b 100644 --- a/tinyserver.js +++ b/tinyserver.js @@ -1,3 +1,12 @@ +/* + * tinyserver.js + * + * author: Kyle + * + * teh server, what else is new? + * + */ + var http = require('http'); var util = require('util'); var url = require('url'); @@ -14,26 +23,26 @@ var activeUsers = []; /* this function is poked upon 'request' event */ function requestListener(req, res) { - var datareq = ''; + var reqdata = ''; /* for POST data */ - req.on('data', function(chunk) { datareq += chunk; } ); + req.on('data', function(chunk) { reqdata += chunk; } ); + req.on('close', function() { util.log('[reqlistener] client terminated before we could respond'); }); req.on('end', function() { - util.log('[reqlistener] finished recv data'); - //console.log('data: %s', datareq.length == 0 ? 'empty' : datareq); + util.log(util.format('[reqlistener] finished recv data%s', (reqdata.length == 0) ? '' : ', got ' + reqdata.length + ' bytes')); /* should be some logic, e.g. blueprint 1 */ util.log('[reqlistener] request kind: ' + req.method); /* routing for pathname, figure out what request we have */ var urlquery = url.parse(req.url); //console.log('[reqlistener] query dump:\n' + util.inspect(urlquery)); - router.route(urlquery, res); + router.route(urlquery, reqdata, res); }); - req.on('close', function() { util.log('[reqlistener] client terminated before we could respond'); }); } var server = http.createServer(requestListener); server.listen(serverConfig.port, serverConfig.hostname, function() { /* could use for proxy stuff? this function is called when listening event is emitted */ + util.log('[server] listening on port ' + serverConfig.port); }); server.on('connection', function(socket) { |