summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-06-29 23:39:29 -0500
committerKyle Kaminski <kyle@kkaminsk.com>2012-06-29 23:39:29 -0500
commita1d9689c5df0bdaf49a42e4682742e344665791a (patch)
tree3ee92c36cc55e41f279ab8ee7aaab83daa739548
parent14f7c610cbf281b5f5776b84b452baf5dfc31f52 (diff)
downloadfubar-a1d9689c5df0bdaf49a42e4682742e344665791a.tar.gz
fubar-a1d9689c5df0bdaf49a42e4682742e344665791a.tar.bz2
fubar-a1d9689c5df0bdaf49a42e4682742e344665791a.zip
work more on this, what more can I say
-rw-r--r--pnhandler.js59
-rw-r--r--route.js8
-rw-r--r--tinyserver.js21
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;
diff --git a/route.js b/route.js
index e8f614f..9a47ff5 100644
--- a/route.js
+++ b/route.js
@@ -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) {