-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (91 loc) · 2.86 KB
/
Copy pathindex.js
File metadata and controls
111 lines (91 loc) · 2.86 KB
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
var pg = require('pg');
var express = require('express');
var cool = require('cool-ascii-faces');
var mongoose = require('mongoose');
var uriUtil = require('mongodb-uri');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.get('/cool', function(request, response) {
var result = ''
var times = process.env.TIMES || 5
for (i=0; i < times; i++)
result += cool() + "<br/>\n";
response.send(result);
});
app.get('/db', function (request, response) {
console.log("Database URL: " + process.env.DATABASE_URL);
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM test_table', function(err, result) {
done();
if (err)
{ console.error(err); response.send("Error " + err); }
else
{ response.render('pages/db', {results: result.rows} ); }
});
});
});
var options = { server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS : 30000 } } };
var mongodbUri = process.env.MONGOLAB_URI;
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
// Create song schema
var songSchema = mongoose.Schema({
decade: String,
artist: String,
song: String,
weeksAtOne: Number
});
// Store song documents in a collection called "songs"
var Song = mongoose.model('songs', songSchema);
mongoose.connect(mongooseUri, options);
var mdb = mongoose.connection;
mdb.on('error', console.error.bind(console, 'connection error:'));
// mdb.once('open', function callback () {
// // Create seed data
// var seventies = new Song({
// decade: '1970s',
// artist: 'Debby Boone',
// song: 'You Light Up My Life',
// weeksAtOne: 10
// });
// var eighties = new Song({
// decade: '1980s',
// artist: 'Olivia Newton-John',
// song: 'Physical',
// weeksAtOne: 10
// });
// var nineties = new Song({
// decade: '1990s',
// artist: 'Mariah Carey',
// song: 'One Sweet Day',
// weeksAtOne: 16
// });
// /*
// * First we'll add a few songs. Nothing is required to create the
// * songs collection; it is created automatically when we insert.
// */
// seventies.save();
// eighties.save();
// nineties.save();
// });
app.get('/mongodb', function (request, response) {
console.log("Mongo URI: " + mongooseUri);
Song.find({}, function(err, songs){
if(err){
console.log(err);
response.send("Error: " + err);
} else {
response.render('pages/mongodb', {results: songs} );
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});