This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·164 lines (127 loc) · 4.91 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
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
var express = require('express');
var mongoose = require ("mongoose"); // The reason for this demo.
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring =
process.env.MONGODB_URI ||
'mongodb://localhost,localhost:35046';
// The http server will listen to an appropriate port, or default to
// port 5000.
var theport = process.env.PORT || 5000;
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
}
});
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('/db', function(request, response) {
response.render('pages/db');
});
app.get('/about', function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
createWebpage(request, response);
});
// This is the schema. Note the types, validation and trim
// statements. They enforce useful constraints on the data.
var userSchema = new mongoose.Schema({
name: {
first: String,
last: { type: String, trim: true }
},
age: { type: Number, min: 0}
});
// Compiles the schema into a model, opening (or creating, if
// nonexistent) the 'PowerUsers' collection in the MongoDB database
var PUser = mongoose.model('PowerUsers', userSchema);
// Clear out old data
PUser.remove({}, function(err) {
if (err) {
console.log ('error deleting old data.');
}
});
// Creating one user.
var johndoe = new PUser ({
name: { first: 'John', last: 'Doe' },
age: 25
});
// Saving it to the database.
johndoe.save(function (err) {if (err) console.log ('Error on save!')});
// Creating more users manually
var janedoe = new PUser ({
name: { first: 'Jane', last: 'Doe' },
age: 65
});
janedoe.save(function (err) {if (err) console.log ('Error on save!')});
// Creating more users manually
var alicesmith = new PUser ({
name: { first: 'Alice', last: 'Smith' },
age: 45
});
alicesmith.save(function (err) {if (err) console.log ('Error on save!')});
var alicesmith = new PUser ({
name: { first: 'Prabhanshu', last: 'Attri' },
age: 22
});
alicesmith.save(function (err) {if (err) console.log ('Error on save!')});
// In case the browser connects before the database is connected, the
// user will see this message.
var found = ['DB Connection not yet established. Try again later. Check the console output for error messages if this persists.'];
function createWebpage (request, response) {
// Let's find all the documents
PUser.find({}).exec(function(err, result) {
if (!err) {
response.write(html1 + JSON.stringify(result, undefined, 2) + html2 + result.length + html3);
// Let's see if there are any senior citizens (older than 64) with the last name Doe using the query constructor
var query = PUser.find({'name.last': 'Doe'}); // (ok in this example, it's all entries)
query.where('age').gt(64);
query.exec(function(err, result) {
if (!err) {
response.write(html4 + JSON.stringify(result, undefined, 2) + html5 + result.length + html6);
} else {
response.write('Error in second query. ' + err)
}
});
} else {
response.write('Error in first query. ' + err)
};
});
}
// Tell the console we're getting ready.
// The listener in http.createServer should still be active after these messages are emitted.
console.log('http server will be listening on port %d', theport);
console.log('CTRL+C to exit');
//
// House keeping.
//
// The rudimentary HTML content in three pieces.
var html1 = '<title> hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on Heroku </title> \
<head> \
<style> body {color: #394a5f; font-family: sans-serif} </style> \
</head> \
<body> \
<h1> hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on Heroku </h1> \
See the <a href="https://devcenter.heroku.com/articles/nodejs-mongoose">supporting article on the Dev Center</a> to learn more about data modeling with Mongoose. \
<br\> \
<br\> \
<br\> <h2> All Documents in MonogoDB database </h2> <pre><code> ';
var html2 = '</code></pre> <br\> <i>';
var html3 = ' documents. </i> <br\> <br\>';
var html4 = '<h2> Queried (name.last = "Doe", age >64) Documents in MonogoDB database </h2> <pre><code> ';
var html5 = '</code></pre> <br\> <i>';
var html6 = ' documents. </i> <br\> <br\> \
<br\> <br\> <center><i> Demo code available at <a href="http://github.com/mongolab/hello-mongoose">github.com</a> </i></center>';
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});