summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-07-01 13:52:34 -0500
committerKamil Kaminski <kyle@kkaminsk.com>2012-07-01 13:52:34 -0500
commit41946c6982647abec5d64b04323a081c73c43577 (patch)
tree32fbfd0e636aa6ba2c19bf153295466728861ec7
parente697192c6232fabc5f218510795464813cff4f1e (diff)
downloadfubar-41946c6982647abec5d64b04323a081c73c43577.tar.gz
fubar-41946c6982647abec5d64b04323a081c73c43577.tar.bz2
fubar-41946c6982647abec5d64b04323a081c73c43577.zip
db2 with session and gzip
-rw-r--r--db2.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/db2.js b/db2.js
new file mode 100644
index 0000000..fcdc456
--- /dev/null
+++ b/db2.js
@@ -0,0 +1,96 @@
+/* creates users with name and password and is able to list them
+ *
+ * $ mongo; use nodejs1; db.getCollectionNames(), db.users.find(), db.users.remove(), db.users.drop()
+ *
+ */
+
+var express = require('express');
+var RedisStore = require('connect-redis')(express);
+var dburl = 'localhost/nodejs1';
+var collections = ['users'];
+var db = require("mongojs").connect(dburl, collections);
+
+var app = express.createServer();
+
+app.configure(function() {
+ app.use(express.logger('dev'));
+ app.use(express.favicon());
+ app.use(express.compress()); /* gzip */
+ app.use(express.bodyParser()); /* creates req,body which req.param() uses */
+ app.use(express.cookieParser()); /* req.session can be populated with user defined vars */
+ app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));
+ app.use(app.router);
+});
+
+app.get('/', function(req, res) {
+ var views = 0;
+ if (req.session.views != 'undefined' && req.session.views)
+ views = req.session.views;
+
+ res.send('<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<body>\n' +
+ '<p>\n' +
+ ' create users <a href="/user">here</a><br />\n' +
+ ' list users <a href="/list">here</a><br />\n' +
+ 'this page has been viewed: ' + views + '\n' +
+ '</p>\n' +
+ '</body>\n' +
+ '</html>\n');
+});
+
+app.get('/user', function(req, res) {
+ req.session.views++;
+ res.send('<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<body>\n' +
+ '<form method="post" action="/create">\n' +
+ ' User: <input type="text" name="username" /><br />\n' +
+ ' Password: <input type="text" name="password" /><br />\n' +
+ ' <input type="submit" value="add" />\n' +
+ '</form>\n' +
+ '</body>\n' +
+ '</html>\n');
+});
+
+app.post('/create', function(req, res) {
+ db.users.save({user: req.param('username'), pass: req.param('password')}, function(err, thing) {
+ if (err || !thing)
+ console.log('error saving');
+ else
+ console.log('successfully saved');
+ });
+ res.redirect('/');
+});
+
+app.get('/list', function(req, res) {
+ db.users.find(function(err, things) {
+ /* things should be array of structs heh */
+
+ if (err || !things)
+ console.log('nothing in db or error retrieving');
+ else
+ {
+ var userlist = '';
+ things.forEach(function(user) {
+ userlist = userlist + 'User: ' + user.user + '<br />Password: ' + user.pass + '<br />';
+ });
+
+ res.send('<!DOCTYPE html>\n' +
+ '<html>\n' +
+ '<body>\n' +
+ userlist +
+ '</body>\n' +
+ '</html>\n');
+ }
+
+ });
+});
+
+app.listen(8081, function() {
+ console.log("listening on port %d in %s mode", this.address().port, this.settings.env);
+})
+.on('error', function(e) {
+ console.log('failed creating server, errno: ' + e.errno);
+});
+