diff options
author | Kyle K <kylek389@gmail.com> | 2012-07-01 06:31:30 -0500 |
---|---|---|
committer | Kyle Kaminski <kyle@kkaminsk.com> | 2012-07-01 06:31:30 -0500 |
commit | e697192c6232fabc5f218510795464813cff4f1e (patch) | |
tree | 00a58bd532c994e85a620065f34950065099e052 | |
parent | 8263f26456dbcd7d5871fc5b394d08af8f4f63b4 (diff) | |
download | fubar-e697192c6232fabc5f218510795464813cff4f1e.tar.gz fubar-e697192c6232fabc5f218510795464813cff4f1e.tar.bz2 fubar-e697192c6232fabc5f218510795464813cff4f1e.zip |
first db sample using mongodb
-rw-r--r-- | db1.js | 86 | ||||
-rw-r--r-- | notes.txt | 4 | ||||
-rw-r--r-- | simpleserver.js | 21 |
3 files changed, 111 insertions, 0 deletions
@@ -0,0 +1,86 @@ +/* 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 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.bodyParser()); /* creates req,body which req.param() uses */ + app.use(app.router); +}); + +app.get('/', function(req, res) { + 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>\n' + + '</p>\n' + + '</body>\n' + + '</html>\n'); +}); + +app.get('/user', function(req, res) { + 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); +}); + @@ -3,3 +3,7 @@ $ express do '# npm install -g express' beforehand, express command generates nice template dir structure to get started +[code analysis] +# npm install -g jslint +$ jslint --sloppy --white <file.js> + diff --git a/simpleserver.js b/simpleserver.js new file mode 100644 index 0000000..b90d6ea --- /dev/null +++ b/simpleserver.js @@ -0,0 +1,21 @@ +var express = require('express'); + +var app = express.createServer(); + +app.configure(function() { + app.use(express.logger('dev')); + app.use(express.favicon()); + app.use(app.router); +}); + +app.get('/*', function(req, res) { + res.send('hello, world!\n'); +}); + +app.listen(8080, 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); +}); + |