-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
148 lines (120 loc) · 3.67 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var express = require('express');
var Tab = require(__dirname + '/models/tab').Tab;
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
var tabProvider= new Tab({
host: 'flame.mongohq.com',
port: 27087,
db: 'app3708478',
username: process.env.MONGO_USR,
password: process.env.MONGO_PASS
});
/**
*
* We'll keep it nice and simple with the routes all defined here
* in the future we might implement an MVC structure
*
**/
app.get('/', function(req, res){
//get the 20 most popular tabs
tabProvider.findPopular(20, function(error,docs){
//strip the " Tab" from the titles - clean the DB later to speed this up
var tabs = [];
if (docs !== undefined && docs.length > 0)
{
docs.forEach(function(tab){
tabs.push({
title: tab.title.replace(' Tab', ''),
artist: tab.artist,
created_on: tab.created_on,
_id: tab._id
});
});
}
//render the index page
res.render('index.jade', { locals: {
title: 'AwesomeTabs',
search_placeholder: 'Search',
tabs:tabs
}
});
});
});
app.get('/tab/:id', function(req, res) {
//get this tab
tabProvider.findById(req.params.id, function(something, doc){
//render the view page
res.render('view.jade', { locals: {
title: 'AwesomeTabs',
search_placeholder: 'Search',
tab: {
title: doc.title.replace(' Tab', ''),
artist: doc.artist,
created_on: doc.created_on,
text: doc.text.replace('<br />', '')
}
}
});
});
});
app.post('/search', function(req, res) {
var keywords = req.param('search').split(new RegExp("(-|by| )"));
search_keywords = [];
keywords.forEach(function(keyword){
if (keyword.length > 0 && keyword != ' ' && keyword != '-')
search_keywords.push(keyword);
});
tabProvider.findByTitleOrArtist(search_keywords, function(something, docs){
//strip the " Tab" from the titles - clean the DB later to speed this up
var tabs = [];
if (docs !== undefined && docs.length > 0)
{
docs.forEach(function(tab){
tabs.push({
title: tab.title.replace(' Tab', ''),
artist: tab.artist,
created_on: tab.created_on,
_id: tab._id
});
});
}
//render the search page
res.render('search.jade', { locals: {
title: 'AwesomeTabs',
search_placeholder: req.param('search'),
searched: req.param('search'),
tabs: tabs
}
});
});
});
app.get('/tab/new', function(req, res) {
res.render('tab_new.jade', { locals: {
title: 'New Tab'
}
});
});
app.post('/tab/new', function(req, res){
tabProvider.save({
artist: req.param('artist'),
'title': req.param('title'),
text: req.param('text')
}, function( error, docs) {
res.redirect('/');
});
});
app.listen(process.env.PORT || 3000);