summaryrefslogtreecommitdiffstats
path: root/db1.js
blob: 63079235951ca726ef9b8bd7780ff5e3d569ef1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
});