-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
163 lines (138 loc) · 3.55 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Module dependencies.
*/
var express = require('express');
var sys = require('sys');
var app = module.exports = express.createServer();
var Counter = require('./models/counter');
var Article = Counter.Article;
var util = require('util');
//var Solr = require('./models/solr');
//var SolrQ = Solr.Q;
// Configuration
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());
});
//show search page
app.get('/search', function(req, res) {
res.render('search', {title: 'Search Now!'});
});
app.post('/search', function(req, res) {
var ss = req.param('ss', null);
var solr = require('solr');
var client = solr.createClient();
var responseObject= {message: 'No results found!'};
client.query(ss, function(err, data) {
responseObject = JSON.parse(data);
//console.log(Q);
res.render('showResults', {
title: 'Showing results for: ' + ss,
results: responseObject
});
});
});
//the view
app.get('/articles', function(req, res) {
Article.find({}, function(err, docs) {
res.render('article', {
title: 'List of articles',
articles: docs
});
});
});
// New article
app.get('/articles/new', function(req, res){
res.render('new', {
title: 'New Article'
});
});
// View an article
app.get('/article/:id', function(req, res){
Article.findOne({_id:req.params.id}, function(err,article){
res.render('show', {
title: article.doc.title,
article: article.doc
});
});
});
// Create/Update articles
app.post('/articles', function(req, res){
if(req.body.article._id)
Article.findOne({_id:req.body.article._id}, function(err, a) {
a.title = req.body.article.title;
a.body = req.body.article.body;
a.save(function(err) {
console.log(err);
})
});
else {
article = new Article(req.body.article);
article.save(function(err){
console.log("Created");
});
}
res.redirect('/articles');
});
app.get('/count', function(req, resp) {
/*
Counter.count(function(num_records) {
if(num_records > 0) {
var c = new Counter();
c.num = 0;
c.save(function() {
res.render('count', {locals: {count: c.num}});
})
} else {
Counter.find().last(function(c) {
c.num += 1;
c.save(function() {
res.render('count', {locals: {count: c.num}});
});
});
}
});
*/
});
app.get('/', function(req, res){
res.render('index', {
title: 'EricM rocks',
title1: 'Brian rocks too!'
});
//var bl = util.inspect(req._events, false, 2);
//console.log(bl);
});
app.get('/about', function(req, res){
res.render('about', {title: 'About Us'});
});
app.get('/contact', function(req, res) {
res.render('contact', {title: 'You can contact me via:'});
});
app.get('/login', function(req, res) {
var user = {email: "[email protected]"};
res.render('login', {title: 'Please Login:'});
});
app.post('/login', function(req, res) {
var password = req.param('password', null);
if(password && password == 'letmein'){
res.redirect('about');
} else {
res.redirect('login');
}
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}