-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
71 lines (57 loc) · 2.01 KB
/
app.js
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
var express = require('express');
function apiapp (db){
var app = express();
var clips = db.collection('clips');
app.use(express.bodyParser());
app.post('/clip', function(req,res,next) {
var password = req.body.password;
var user = req.body.user;
if(password != process.env.API_TEST_PASSWORD){
res.set('Content-Type', 'text/plain').send(403,
'If you want to insert a clip, you must speak the secret word');
} else if(!user){
res.set('Content-Type', 'text/plain').send(403,
'Clips need to be attached to a given username' );
} else {
user = 'test~'+user;
var html = req.body.html || '';
var desc = req.body.desc || '';
// NOTE: In a proper API, this should be set by the ID of the API
// consumer corresponding to the key used to authenticate the method
var source = req.body.source || 'anonymous';
clips.insert({user: user, html: html.trim(),
desc: desc.trim(), source: source}, {safe: true},
function(err,doc) {
if(err) return next(err);
res.set('Content-Type', 'text/plain')
.send(200,'Inserted:\n\n'+JSON.stringify(doc));
});
}
});
return app;
}
module.exports = function appctor(db){
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
var clips = db.collection('clips');
app.use(express.static(__dirname+'/www'));
app.use('/api/v0',apiapp(db));
app.get('/api/v0/clip',function(req, res, next){
res.render('manuclip.jade');
});
app.get('/:user',function(req, res, next){
clips.find({user: req.params.user}, function(err,cursor) {
if (err) return next(err);
else cursor.toArray(function(err,userclips){
if (err) return next(err);
//TODO: Check for user entry, not user clips
else if (userclips.length == 0) return next();
else res.render('userclips.jade',{clips: userclips});
});
});
});
app.get('/:user/tags/:tag',function(req,res){});
app.get('/:user/:page',function(req,res){});
return app;
};