-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (49 loc) · 1.26 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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var server = app.listen(6969);
var staticDir = __dirname + '/public/';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.sendFile(staticDir + 'index.html');
});
app.get('/messages', function (req, res) {
showOldMsgs(res);
});
app.post('/messages', function (req, res) {
var message = req.body;
res.json(message);
saveMsg(message, function(err){if(err) throw err;});
});
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/chatappajax', function(err){
if(err) {
console.log(err);
} else {
console.log('Connected to mongodb!');
}
});
var chatSchema = mongoose.Schema({
name: String,
text: String,
created: {type: Date, default: Date.now}
});
var Chat = mongoose.model('message', chatSchema);
function showOldMsgs(res){
getOldMsgs(69, function(err, docs){
res.json(docs);
});
}
var getOldMsgs = function(limit, cb){
var query = Chat.find({});
query.sort('created').limit(limit).exec(function(err, docs){
cb(err, docs);
});
}
var saveMsg = function(data, cb){
var newMsg = new Chat({name: data.name, text: data.text});
newMsg.save(function(err){
cb(err);
});
};