forked from DaoCloud/node-mongo-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (51 loc) · 1.71 KB
/
server.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
var express = require('express');
var moment = require('moment');
var mongoose = require('mongoose');
// Constants
var PORT = 80;
// App
var app = express();
var port = process.env.MONGODB_PORT_27017_TCP_PORT;
var addr = process.env.MONGODB_PORT_27017_TCP_ADDR;
var instance = process.env.MONGODB_INSTANCE_NAME;
var password = process.env.MONGODB_PASSWORD;
var username = process.env.MONGODB_USERNAME;
// 'mongodb://user:pass@localhost:port/database'
mongoose.connect('mongodb://' + username + ':' + password +'@' + addr + ':' + port + '/' + instance);
var Records = mongoose.model('Records', { name: {type: String, default:'any'}, time: {type: Date, default: Date.now} });
app.get('/', function (req, res) {
//res.send('Hello world\n');
var record = new Records({ name: req.ip });
record.save(function (err) {
if (err) {
console.log(err)
} else {
console.log('save');
}
});
Records.find(function (err, docs) {
if(err) {
console.log(err);
} else {
var out = '<table border="1" align="center" width="50%"> <thead> <tr> <th> IP </th> <th> time </th></tr></thead> <tbody> ';
for (var i = 0,l = docs.length; i < l; i++){
out = out + '<tr> <th>' + docs[i].name + '</th> <th>' + moment(docs[i].date).format() + '</th></tr>';
}
out = out + '</tbody> </table>'
res.send(out);
}
});
});
app.get('/drop', function (req, res) {
Records.remove({}, function(err) {
if(err) {
console.log(err);
res.send('drop collection Records failed');
} else {
res.send('drop collection Records success');
console.log('drop collection Records success');
}
});
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);