-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
120 lines (105 loc) · 12.7 KB
/
app.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
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
expressSanitizer = require("express-sanitizer"),
mongoose = require("mongoose"),
methodOverride = require("method-override");
//APP CONFIG
var url = process.env.DATABASEURL || "mongodb://localhost/student_app";
mongoose.connect(url);
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressSanitizer());
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(methodOverride("_method"));
//MONGOOSE/MODEL CONFIG
var studentSchema = new mongoose.Schema({
name: String,
age: Number,
passport: String,
sex: String,
created: {type: Date, default: Date.now}
});
var Student = mongoose.model("Blog", studentSchema);
// Student.create({
// name: "Jerry",
// age: 24,
// sex: "Male",
// passport: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUSEhIWFRUVFRcVFRUVFRUVFRUVFRUWFhUVFRUYHSggGBolHRUVITEhJSkrLi4uFx8zODUtNygtLisBCgoKDg0OGhAQGi0lHx0tLS0tLS0vLS0tLS0tLS0rLS0tLS0tLS0tLS0tLS0tLS0rLS0tLS0tLS0tLS0tLS0tLf/AABEIAMUBAAMBIgACEQEDEQH/xAAcAAABBAMBAAAAAAAAAAAAAAAEAwUGBwABAgj/xABGEAABAwIEAwcCAgYHBQkAAAABAAIDBBEFEiExBkFRBxMiYXGBkaGxMsEUQlJyktEVIzNiguHxJEOToqMIFiU0Y2SywvD/xAAaAQADAQEBAQAAAAAAAAAAAA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+Ub+gdqYyfI6t9cq6/pmbTl0v8AyVFCuqjdcsOqXfHmb0I0N/JCMK+iCLSNQ00QcLIkOSUg6IGGsktuCiZTpfpYrVW2/qEgH3bbyShC2uSglshYnXASr1jC9JJoT6/klM4OyGH4B53STJcp8kbNQa7Y+i5BXImHJJCXkiAW8whag7rpz3cklM7Q3QYUGcO4HNWziCADMRdxOjWNFrud5aj5Xo/hDhuHDKfu43XcdZHnd7jz8hpYDkob2M4THDRuqXkCSocbEm1o2Eta0XPUOPuOintLUQtu98rbD9pzbAdd1zTm7pHXjglG2CYpxI1gOjnE6BrQXOcfJo1KYpY6qqY/9Jc6nhIIyNLRIQRbV2oaNfNccQ9p9HCXNgBmPWJoyk/v6C3yq14j7QZ6huTWEHe3iNvVBY5vkd5YJEVeGskdY3a1zmsvzaCQHHzIsnuhrWvFmnXomSCOF27nX87WP8kaaQjWPKB1A19yutWcDHV05tso5ijf6y/UD+X5J3bK4Cz0BibdWnqD9/8ANaQEN7VsrQXRSjGmhLtCGaUQHLIDHfh2pyTNKunCqnNGCqFo5LPafNXTw1LeMei8r6lHiROfI8ulXJlST1wZAvI1iB0chSeKUrZ4ZIXjwyNLT1FxoR5g2PssWKabW6LFDxHMMrtxz5oZ7bG11ixfZdjHYctXW1iwRCrHPqgRuVpYgwo3C+y33xKxYgEVDzey4cVixYwvSR5jlvb6o5tG1u5J+n2WliZCsVdG0/qj2Q8kABFuvMXPsTssWIgDsgO4B5ahLmFtr2HwsWJkBm4xr1Q9W4XsWghaWLAB58PYRcac0PHK6M2BuPNYsSsZG624cCCdRex1t5XXM0l2C/IkfIH8lixL3MBrpYsQGETulWFbWIGO2u1Hqra4PqT3Y9FtYvP+p/20TmPk8hSJeVixeBEkf//Z"
// });
//RESTFUL ROUTES
app.get("/", function(req, res) {
res.redirect("/students");
});
//INDEX ROUTE
app.get("/students", function(req, res){
Student.find({}, function(err, students){
if(err){
console.log("ERROR!");
} else {
res.render("index", {students: students});
}
});
});
//NEW ROUTE
app.get("/students/new", function(req, res) {
res.render("new");
});
//CREATE ROUTE
app.post("/students", function(req, res){
//create new student
req.body.student.name = req.sanitize(req.body.student.name);
Student.create(req.body.student, function(err, newStudent){
if(err){
res.render("new");
} else {
//then redirect to the index
res.redirect("/students");
}
});
});
//SHOW ROUTE
app.get("/students/:id", function(req, res) {
Student.findById(req.params.id, function(err, foundStudent){
if(err){
res.redirect("/students");
} else {
res.render("show", {student: foundStudent});
}
});
});
//EDIT ROUTE
app.get("/students/:id/edit", function(req, res) {
Student.findById(req.params.id, function(err, foundStudent){
if(err){
res.redirect("/students");
} else {
res.render("edit", {student: foundStudent});
}
});
});
//UPDATE ROUTE
app.put("/students/:id", function(req, res){
req.body.student.name = req.sanitize(req.body.student.name);
Student.findByIdAndUpdate(req.params.id, req.body.student, function(err, updatedStudent){
if(err){
res.redirect("/students")
} else {
res.redirect("/students/" + req.params.id);
}
});
});
//DELETE ROUTE
app.delete("/students/:id", function(req, res){
Student.findByIdAndRemove(req.params.id, function(err){
//destroy student resource
if(err){
res.redirect("/students");
} else {
res.redirect("/students");
}
});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("StudentsApp Server has started!");
});