-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·106 lines (78 loc) · 2.47 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
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
var http = require("http");
var express = require("express");
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongoose = require('mongoose');
var path = require('path');
var bodyParser = require("body-parser");
var urlencodedParser = bodyParser.urlencoded({ extended: false});
var todoModel = require('./model/schemaTodo');
mongoose.connect('mongodb://localhost/Todolist', function(err) {
if (err) { throw err; }else{
console.log("MONGODB connected")
}
});
app.use(bodyParser.urlencoded({ extended: true }));
// Parse le JSON. Ajout pas possible sans cette ligne
app.use(bodyParser.json({ type: 'application/json' }));
// Sert les fichiers (CSS, HTML, JS)
app.use("/assets", express.static("client/static"));
// Ont sert les controller,service,template (Angular JS)
app.use("/app", express.static("client/app"));
//Route qui gère la page index
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + "/client/index.html"));
});
// Route API qui affiche notre liste de tâche
app.get('/todo', function (req, res) {
todoModel.find({}, function (err, comms) {
if (err) {
throw err;
} else {
res.json(comms);
}
// comms est un tableau de hash
})
});
// Route API qui crée une nouvelle tâche
app.post('/todo', function(req, res) {
var todo1 = new todoModel(req.body);
todo1.save(function(err, todo) {
if(err)
{
console.log(err);
}
else
{
//console.log(todo);
io.emit('newTodo', todo);
res.send(todo);
}
});
});
// Route API qui supprime une tâche
app.delete('/todo/:id', function (req, res) {
console.log(req.body);
todoModel.remove({ "_id": req.params.id }, function(err, todo) {
}).then(function(succ) {
//console.log(succ);
io.emit('deleteTodo', { '_id' : req.params.id});
res.send(req.body);
}, function(err) {
res.send(err);
});
});
//Route API qui change l'état d'une tâche
app.put('/todo/:id', function (req, res) {
console.log(req.body);
todoModel.findOneAndUpdate({ "_id": req.body._id }, req.body, function(err, todo) {
}).then(function(succ) {
//console.log(succ);
io.emit('updateTodo', req.body);
res.send(req.body);
}, function(err) {
res.send(err);
});
});
http.listen(8080);