-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (96 loc) · 3.09 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Module dependencies.
*/
var config = require('./config');
var dc = require('./lib/documents_collection');
var express = require('express');
var jsv = require('./lib/jsonview.js');
var mongodb = require("mongodb");
var stache = require('./lib/stache');
var tc = require('./lib/tweets_collection');
var app = module.exports = express.createServer();
var db = new mongodb.Db('veille', new mongodb.Server("127.0.0.1", 27017, {}));
// Configuration
app.configure(function(){
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
//app.set('view options', {layout: false});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(express['static'](__dirname + '/public'));
app.use(app.router);
app.register(".html", require(__dirname + '/lib/stache')); // For Mustache
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get(/^\/(documents|tweets)(?:\/(.+$))?/, function(req, res){
var objName = req.params[0];
var colName = objName[0].toUpperCase() + objName.substring(1) + 'Collection';
var col = require('./lib/' + objName + '_collection')[colName];
col = new col(db);
if (req.params[1]) {
var method = (config.data.fetch_missing === true ? 'getOrFetch' : 'get');
col[method](req.params[1], function(doc) {
if (!doc) {
res.send('No ' + objName + ' with id ' + req.params[1], 404);
} else {
var jsonFormatter = new jsv.JSONFormatter();
res.render(objName + '/view.html', {
locals: {
doc: doc,
escaped: jsonFormatter.jsonToHTML(doc)
},
partials: {},
layout: !req.xhr
});
}
}, function(){
});
} else {
col.index(function(documents) {
res.render(objName + '/index.html', {
locals: {
documents: documents
},
partials: {},
layout: !req.xhr
});
}, function(){
});
}
});
app.get(/^\/(alchemy|amplify|calais)(?:\/(.+$))/, function(req, res){
var dc = require('./lib/documents_collection');
var col = new dc.DocumentsCollection(db);
col.getOrFetch(req.params[1], function(doc) {
var tmpDoc = {
url: doc.url,
text: doc.viewtext.content
};
var lib = require('./lib/' + req.params[0] + '.js');
lib = new lib(config.data[req.params[0]]);
lib.get(tmpDoc, function(doc){
var jsonFormatter = new jsv.JSONFormatter();
res.render('test/view.html', {
locals: {
escaped: jsonFormatter.jsonToHTML(doc)
},
partials: {},
layout: !req.xhr
});
});
});
});
app.get('*', function(req, res){
res.send('404', 404);
});
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);