summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-06-30 14:30:41 -0500
committerKyle Kaminski <kyle@kkaminsk.com>2012-06-30 14:30:41 -0500
commitd795d12618bc816c547fc6fd50b167c481be9e22 (patch)
tree3c5aa791b5e32acd5431546a04e5fe79cdcc2267
parentf7c4894f42d520126ba957f78d9f888feef32426 (diff)
downloadfubar-d795d12618bc816c547fc6fd50b167c481be9e22.tar.gz
fubar-d795d12618bc816c547fc6fd50b167c481be9e22.tar.bz2
fubar-d795d12618bc816c547fc6fd50b167c481be9e22.zip
add some express samples, and work more
-rw-r--r--express/app1.js24
-rw-r--r--express/app2.js55
-rw-r--r--pnhandler.js23
-rw-r--r--route.js1
-rw-r--r--samples/func_ret_func.js12
-rw-r--r--tinyserver.js22
6 files changed, 132 insertions, 5 deletions
diff --git a/express/app1.js b/express/app1.js
new file mode 100644
index 0000000..8c25531
--- /dev/null
+++ b/express/app1.js
@@ -0,0 +1,24 @@
+var express = require('express');
+
+var app = express.createServer()
+
+app.configure('dev', function() {
+ app.use(express.errorHandler({
+ dumpExceptions: true,
+ showStack: true
+ }));
+});
+
+app.configure(function() {
+ app.use(express.logger('dev'));
+ app.use(express.favicon());
+});
+
+app.get('/foo', function(req, res) {
+ res.send('hello, foo!\n');
+})
+app.get('/', function(req, res) {
+ res.send('hello, world!\n');
+})
+app.listen(8081);
+
diff --git a/express/app2.js b/express/app2.js
new file mode 100644
index 0000000..db95537
--- /dev/null
+++ b/express/app2.js
@@ -0,0 +1,55 @@
+var express = require('express');
+var util = require('util');
+
+var app = express.createServer()
+var pagehits = 0;
+
+app.configure('dev', function() {
+ app.use(express.errorHandler({
+ dumpExceptions: true,
+ showStack: true
+ }));
+});
+
+app.configure(function() {
+ app.use(express.logger('dev'));
+ app.use(express.favicon());
+
+ app.use(express.methodOverride());
+ /* parse request bodies, place the result in req.body */
+ app.use(express.bodyParser());
+ app.use(app.router);
+ var oneYear = 31557600000;
+ app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
+
+ app.set('views', __dirname + '/views');
+});
+
+app.get('/*', function(req, res, next) {
+ pagehits++;
+ /* look at app.use*, app.router brings us here since we have a GET match here,
+ * we need to call next() so we pass control to whoever is next
+ */
+ next();
+});
+
+app.get('/info', function(req, res) {
+ res.send('page hits: ' + pagehits + '\n');
+});
+
+app.get('/user/:id([0-9]+)', function(req, res) {
+ res.send('user ' + req.params.id);
+});
+
+app.get('/', function(req, res) {
+ res.send('hello, world!\n');
+});
+
+app.use(function(req, res) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write('resorting to connect! could do my original routing\n');
+ res.end();
+});
+
+app.listen(8081);
+
diff --git a/pnhandler.js b/pnhandler.js
index dba03db..63637ab 100644
--- a/pnhandler.js
+++ b/pnhandler.js
@@ -39,6 +39,28 @@ function upload(query, reqdata, res) {
res.end();
}
+function info(query, reqdata, res) {
+ util.log('[pnhandler] handling ' + query.pathname);
+
+ var body =
+ '<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<head>\n' +
+ '<title>Info</title>\n' +
+ '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n' +
+ '</head>\n' +
+ '<body>\n' +
+ '<h1 style="background-color: gray; color: lime">Node.JS Server Info</h1>' +
+ 'Node ' + process.version + ', uptime ' + process.uptime() + ' seconds' + '<br />' +
+ 'Memory usage: ' + process.memoryUsage().heapTotal / (1024*1204) + 'MB<br />' +
+ '</body>\n' +
+ '</html>\n';
+
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write(body);
+ res.end();
+}
+
function hello(query, reqdata, res) {
util.log('[pnhandler] handling ' + query.pathname);
@@ -89,6 +111,7 @@ exports.register = register;
exports.login = login;
exports.poll = poll;
exports.upload = upload;
+exports.info = info,
exports.hello = hello;
exports.hellores = hellores;
diff --git a/route.js b/route.js
index 9a47ff5..0b25fa4 100644
--- a/route.js
+++ b/route.js
@@ -15,6 +15,7 @@ var handle = {
'/login': pnhandler.login,
'/poll': pnhandler.poll,
'/upload': pnhandler.upload,
+ '/info' : pnhandler.info,
'/hello': pnhandler.hello,
'/hellores': pnhandler.hellores
};
diff --git a/samples/func_ret_func.js b/samples/func_ret_func.js
new file mode 100644
index 0000000..b4b5a09
--- /dev/null
+++ b/samples/func_ret_func.js
@@ -0,0 +1,12 @@
+/* very fucking weird!
+ */
+
+function retFunction(foo) {
+ return function bar() {
+ console.log(foo);
+ }
+}
+
+var foo = new retFunction('hello, world!');
+foo.call(foo);
+
diff --git a/tinyserver.js b/tinyserver.js
index ac2560b..31315dd 100644
--- a/tinyserver.js
+++ b/tinyserver.js
@@ -7,11 +7,12 @@
*
*/
-var http = require('http');
-var util = require('util');
-var url = require('url');
+var http = require('http');
+var util = require('util');
+var url = require('url');
+var express = require('express');
-var router = require('./route');
+var router = require('./route');
var serverConfig = {
port: 8080,
@@ -39,7 +40,18 @@ function requestListener(req, res) {
});
}
-var server = http.createServer(requestListener);
+//var server = http.createServer(requestListener); /* vanilla api */
+var server = express.createServer();
+server.configure(function() {
+ server.use(express.logger('dev'));
+ server.use(express.favicon());
+ server.use(express.bodyParser()); /* parses body of http? --> populates req.body, hmmm */
+ server.use(express.cookieParser());
+ server.use(express.session({ secret: 'keyboard cat'}));
+ /* server.use(express.static(__dirname + '/public')); */
+});
+server.use(requestListener); /* using connect module here */
+
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);