-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
221 lines (177 loc) · 6.02 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var express = require('express');
var bodyParser=require("body-parser");
var countries=require("countries-list");
// upload dependances
// Env
require('dotenv').config();
const MongoClient = require('mongoose');
const fs=require('fs')
var multer=require("multer")
const path=require("path")
// email pulgin
var nodemailer = require('nodemailer');
//mail functions
var transporter = nodemailer.createTransport({
service: process.env.SERVICE_MAIL, // à definir dans .env
auth: {
user: process.env.EMAIL_ADDRESS,// à definir dans .env
pass: process.env.EMAIL_PASSWORD// à definir dans .env
}
});
// for only one person
function getMailOptions(recipients,message,subject) {
var isHtml=message[0]=="<"
var options={
from: process.env.EMAIL_ADDRESS,
to: recipients,
subject: subject,
text: message,
}
isHtml? options['html']=message:options['text']=message
}
// sendMail function
function sendMails(to) {
var mailOptions=getMailOptions(to, //ecrivez votre mail ici pour recevoir le mail
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum",
"BRANCH");
transporter.sendMail(mailOptions, function(error, info){
if (error) {
return error;
} else {
return 'Email sent: ';
}
});
}
// engine
var app = express();
let imageShema = {
name:String,
img: {data:Buffer,contentType:String},
}
let infosShema = {
name:String,
age:Number,
phone: Number,
email:String,
sexe: String,
nationality:String,
niveauEtude: String,
occupationActuelle: String,
experienceProgramation: String,
coursPrepa: String,
disponibilite: String,
exigence: String,
niveauAnglais: String,
bio: String
}
// Mongo db, nom de tcollection: infos
let Informations=MongoClient.model("infos",infosShema)
let Image=MongoClient.model('images',imageShema)
const uri =process.env.DATABASE_URL;
MongoClient.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage });
// help getting body
app.use(bodyParser.urlencoded({extended: false}));
// indexing static files
app.use("/src/style",express.static(__dirname+"/src/style"));
app.use("/src/image",express.static(__dirname+"/src/image"));
app.use("/src/js",express.static(__dirname+"/src/js"));
// '/' est la route racine
app.get('/', function (req, res) {
res.render(__dirname+"/src/views/welcome.ejs");
});
// route tableau de bord
app.get("/dashboard/:page",function(req,res) {
var perPage = 20
var page = req.params.page || 1
Informations
.find({})
.skip((perPage * page) - perPage)
.limit(perPage)
.exec(function(err, personFound) {
Informations.count().exec(function(err, count) {
if (err) return err
res.render(__dirname+"/src/views/dashboard.ejs", {
data:personFound,
current: page,
pages: Math.ceil(count / perPage)
})
})
});
})
const countryCodes = Object.keys(countries.countries);
const countryNames = countryCodes.map(code => countries.countries[code].name);
// route inscription
app.get('/inscription', function (req, res) {
res.render(__dirname+"/src/views/index.ejs",{data: countryNames,message:{success:"",error:"",warning:""}});
});
// route tableau de bord
app.get("/:page?",function(req,res) {
var perPage = 20
var page = req.params.page || 1
Informations
.find({})
.skip((perPage * page) - perPage)
.limit(perPage)
.exec(function(err, personFound) {
Informations.count().exec(function(err, count) {
if (err) return err
res.render(__dirname+"/src/views/hello.ejs", {
data:personFound,
current: page,
pages: Math.ceil(count / perPage)
})
})
});
})
const countryCodes = Object.keys(countries.countries);
const countryNames = countryCodes.map(code => countries.countries[code].name);
// route inscription
app.get('/inscription', function (req, res) {
res.render(__dirname+"/src/views/index.ejs",{data: countryNames,message:{success:"",error:"",warning:""}});
});
// collect inscription data and save to mongodb database
app.post('/inscription',upload.single("image"), function (req, res) {
var obj = {
name: req.body["email"],
img: {
data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename.split(".")[0])),
contentType:req.file.mimetype
}
}
var photo=new Image(obj)
var document=new Informations(req.body)
document.save(function(err) {
if(err){
res.render(__dirname+"/src/views/index.ejs",{data: countryNames,message:{error: "Impossible de sauvegarder",success:"",warning:""}});
}else{
photo.save( function(err){
if (err) {
console.log(err);
}
});
// res.sendFile(__dirname+"/views/index.html");
sendMails(req.body["email"]);
res.render(__dirname+"/src/views/index.ejs",{data: countryNames,message:{success:"Inscription réussite. Nous vous avons envoyé un mail.",error:"",warning:""}});
}
});
});
//Unmatched routes handler
app.use(function (req, res) {
if (req.method.toLowerCase() === "options") {
res.end();
} else {
res.status(404).type("txt").send("Non trouvé");
}
});
app.listen(4000, function () {
console.log("Application écoute sur le port 4000 !");
});