-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
171 lines (141 loc) · 4.58 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
const http = require("http");
const fs = require("fs");
const express = require('express');
const bodyParser = require('body-parser');
const fileUpload = require("express-fileupload");
const path = require('path');
const { parse } = require('querystring');
const formidable = require('formidable')
const {studentsInfo} = require("./studentsInfo");
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODU_URI || 'mongodb://localhost:27017/StudentsInfo');
const app = express();
console.log(path.join(__dirname, 'public'));
app.set('view engine','ejs');
app.use(express.static(__dirname + '/assets'));
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
app.use(fileUpload());
let individualUser;
app.get('/',(req,res) => {
res.render('students',{studentsInfo})
})
app.get('/students/api',(req, res) => {
res.json(studentsInfo)
});
app.get('/students/api/:id', (req, res) => {
const id = req.params.id;
let flag = false;
for (let i = 0; i < studentsInfo.length; i++) {
if (studentsInfo[i]._id == id) {
individualUser = studentsInfo[i];
console.log(studentsInfo[i].nationality)
flag = true;
res.json(studentsInfo[i]);
break;
}
}
if (!flag) {
console.log('User was not found.')
res.render("notFound")
}
});
app.get('/students/:id', (req, res) => {
const id = req.params.id;
let flag = false;
for (let i = 0; i < studentsInfo.length; i++) {
if (studentsInfo[i]._id == id) {
individualUser = studentsInfo[i];
console.log(studentsInfo[i].nationality)
flag = true;
res.render("student", studentsInfo[i]);
break;
}
}
if (!flag) {
console.log('User was not found.')
res.render("notFound")
}
});
app.get('/add-student',(req,res) =>{
console.log(req.body)
res.render('addStudent')
})
app.post('/students',(req, res) => {
let id = studentsInfo.length + 1;
// console.log('what is this file', req.files.src);
console.log('what is it', req.files);
console.log(req.body);
let skillList = req.body.skills.trim().split(', ');
// console.log(skillList)
if (!req.files) return res
.status(400)
.send("No files were uploaded.");
let sampleFile = req.files.src;
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
// Use the mv() method to place the file somewhere on your server
sampleFile.mv(
__dirname + "/assets/images/" + sampleFile.name,
function(err) {
if (err) return res.status(500).send(err);
req.body._id = id;
req.body.src = sampleFile.name;
req.body.alt = sampleFile.name.slice(0, sampleFile.name.length-4);
req.body.skills = skillList;
studentsInfo.push(req.body);
res.redirect("/");
}
);
// const {_id:id, firstName,lastName,title,nationality, whySoftwareDeveloper, favoriteQuote, src, skilss} = req.body;
// console.log(req.body)
// fs.createWriteStream(__dirname + '/assets/images/' + `${photo}`);
});
app.delete('/students/:id',(req,res) => {
const id = Number(req.params.id);
let flag = true;
for(let i = 0; i < studentsInfo.length; i++){
if(studentsInfo[i]._id === id){
studentsInfo.splice(i,1);
flag = true;
res.json(studentsInfo);
break;
}
}
if(!flag){
console.log('User was not found with this ID.')
res.render("notFound")
}
else{
console.log('A user with Id is deleted');
console.log('Deleted:',deletedUser)
res.render('Student deleted',{student:studentsInfo})
res.send('A user with Id is deleted')
}
});
app.put("/students/edit", (req, res) => {
const id = req.params.id;
let flag = true;
for (let i = 0; i < studentsInfo.length; i++) {
if (studentsInfo[i]._id == id) {
studentsInfo.splice(i, 1);
flag = true;
res.json(studentsInfo);
break;
}
}
if (!flag) {
console.log("User was not found with this ID.");
res.render("notFound");
} else {
console.log("A user with Id is deleted");
console.log("Deleted:", deletedUser);
res.render("Student deleted", { student: studentsInfo });
res.send("A user with Id is deleted");
}
});
console.log(individualUser)
app.listen(port, () => {
console.log(`Server is running on port ${port}....`)
});