-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeetings.js
144 lines (121 loc) · 2.95 KB
/
meetings.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
//gestion de la DB
var mongoose = require('mongoose');
var bodyParser = require('body-parser')
var multer = require('multer');
mongoose.connect('mongodb://localhost/Meetings2');
var MeetingSchema = new mongoose.Schema({
name: String,
description: String,
date: String,
logo: String
});
var Meeting=mongoose.model('Meeting',MeetingSchema);
//gestion du serveur HTTP
var express = require('express');
var app = express();
app.use('/JavaScript', express.static(__dirname + '/app/javascript'));
app.use('/css', express.static(__dirname + '/app/css'));
app.use('/storage', express.static(__dirname + '/app/storage'));
app.use('/lib', express.static(__dirname + '/app/lib'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
app.get('/', function (req, res)
{
res.sendFile(__dirname + '/app/app.html');
});
//multer paramètres du dossier de stockage des logos
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './app/storage/');
},
filename: function (req, file, cb) {
var datetimestamp = Date.now();
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage
}).single('file');
app.post('/upload', function(req, res) {
upload(req,res,function(err){
if(err){
res.json({error_code:1,err_desc:err});
return;
}
res.json({error_code:0,err_desc:null});
});
});
// routes
// affichage des données
app.get('/api/display', function(req, res)
{
Meeting.find(null)
.exec(function(err,result)
{
if (err==true)
{
res.send('err');
}
else
{
res.json(result);
}
})
});
// ajout des données
app.post('/api/form', function(req,res)
{
//création d'un nouveau document destiné à la base
var newMeeting=new Meeting({
name:req.body.name,
description:req.body.description,
date:req.body.date,
logo:req.body.logo
});
//enregistrement dans la base
newMeeting.save(function(err)
{
if (err)
{
res.send('err');
}
else
{
res.send();
}
})
});
// suppression de données
app.delete('/api/delete/:id', function(req, res)
{
Meeting.remove({_id:req.params.id})
.exec(function(err)
{
if (err==true)
{
res.send('err');
}
else
{
res.send('');
}
})
});
//edition de données
app.put('/api/edit/:id',function(req,res)
{
Meeting.update({_id:req.params.id},
{name:req.body.name,description:req.body.description,date:req.body.date, logo:req.body.logo})
.exec(function(err)
{
if (err==true){
res.send('err');
}
else{
res.send();
}
})
});
app.listen(8080, function(){ console.log("Merci d'ouvrir http://localhost:8080")});