-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (91 loc) · 3.16 KB
/
index.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
var express = require('express'),
app = express(),
swig = require('swig'),
nib = require('nib'),
mongojs = require('mongojs'),
stylus = require('stylus'),
async= require('async'),
extend= require('extend'),
yaml = require('yamljs'),
people;
console.log(process.env.MONGO_ADDRESS);
const db = mongojs("mongodb://rieps:[email protected]/hashtagivist", ['hashtags'])
function compile(str, path){
return stylus(str).set('filename',path).use(nib())
}
var settings = yaml.load("settings.yml");
// This is where all the magic happens!
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(stylus.middleware({
src : __dirname + '/public',
compile : compile
}));
app.use(express.static(__dirname + '/public'));
// Swig will cache templates for you, but you can disable
// that and use Express's caching instead, if you like:
app.set('view cache', false);
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
app.use(express.multipart());
console.log(settings);
// To disable Swig's cache, do the following:
swig.setDefaults({
cache: false
});
// NOTE: You should always cache templates in a production environment.
// Don't leave both of these to `false` in production!
var layouts = {};
app.get('/', function (req, res) {
var lists =[];
var columns = [
{ title : "New hashtags", icon : "time", query : { status:"New" } },
{ title : "Trending hashtags", icon : "bullhorn", query : { status: "Trending" }},
{ title : "Successful", icon : "ok", query : { status: "Successful" }}
];
var category = req.query.category || false;
async.map(columns, function(column, done){
var query = extend({}, column.query || {});
if(category) query.category = RegExp(category, 'i');
db.hashtags.find(query).sort(column.sort || { epoch : -1 }).limit(10, function(err, hashtags){
column.hashtags = hashtags;
done(null, column);
});
}, function(err, columns){
console.log(JSON.stringify(columns, 0, ' '));
res.render('index', {
active : category,
lists : columns,
categories : settings.categories
});
});
});
app.get('/submit', function(req, res){
res.render("submit", { categories : settings.categories });
});
app.post('/submit', function(req, res){
console.log(req.body);
db.hashtags.insert({
hashtag : req.body.hashtag,
category : req.body.category,
creator : req.body.creator,
campaign_leader : req.body.campaign_leader,
tweet : req.body.tweet,
description : req.body.description
}, function(err, data){
res.render('submit-success');
});
});
app.get('/hashtag/*', function(req, res){
db.hashtags.findOne({
hashtag : new RegExp(req.params[0], "i")
}, function(err, hashtag){
console.log(req.params[0]);
res.render('hashtag', {
hashtag : hashtag
});
});
});
app.listen(80);
console.log('Application Started on http://localhost:1337/')