-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.js~
108 lines (92 loc) · 2.28 KB
/
server2.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
var express = require('express');
var fs = require('fs');
var https = require('https');
var hbs = require('handlebars');
var router = express.Router();
var path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
});
var taskSchema = new mongoose.Schema({
name: String,
des: String,
due: String,
loc: String
});
var Task = mongoose.model('Task', taskSchema);
mongoose.connect('mongodb://localhost/test');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(require("connect").bodyParser());
var currIdx;
app.get('/addtask', function(req, res){
fs.readFile('./addtask.hbs', function(err, data){
if(err) throw err;
var template = hbs.compile(data.toString());
res.send(template());
});
});
app.get('/details', function(req, res){
fs.readFile('./details.hbs', function(err, data){
if(err) throw err;
var template = hbs.compile(data.toString());
res.send(template());
});
});
app.get('/', function(req,res){
if( req.query.task === undefined){ }
else if ( (req.query.task).length < 1 ) { }
else{
var task = new Task({
name: req.query.task,
des: req.query.des,
due: req.query.due,
loc: req.query.loc
});
task.save(function (err) {
if(err) return console.error(err);
});
}
fs.readFile('./index.hbs', function(err, data){
if(err) throw err;
var template = hbs.compile(data.toString());
res.send(template());
});
});
app.all('/report', function(req,res){
Task.find({}, function(err, records){
console.log(records);
res.send(records);
});
});
app.all('/clear', function(req,res){
if( req.body._id != 'false' ){
Task.remove({_id: req.body._id}, function(err){
if (err) return handleError(err);
});
}
else{
Task.remove({}, function(err){
if (err) return handleError(err);
});
}
});
app.all('/taskinfo', function(req, res){
if (currIdx != undefined) {
Task.find({}, function(err, records){
res.send(records,currIdx);
});
}
Task.count({}, function(err,c) {
if( req.body.idx > -1 && req.body.idx < c ){
currIdx = req.body.idx;
}
else{
currIdx = undefined;
}
});
});
app.listen(8000);