-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (85 loc) · 2.28 KB
/
app.js
File metadata and controls
98 lines (85 loc) · 2.28 KB
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
const bodyparser = require("body-parser"),
methodOverride = require("method-override"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
mongoose.connect("mongodb://localhost:27017/crash_app", {useNewUrlParser: true});
app.set("view engine", "ejs")
app.use(express.static("public"));
// bodyperser get d info from d form (skycolor) here for example
app.use(bodyparser.urlencoded({extended: true}))
app.use(methodOverride("_method"));
let todoSchema = new mongoose.Schema({
list: "String"
});
let Todo = mongoose.model("Todo", todoSchema);
// Todo.create({
// list: "buy tea"
// }, function(err, todo){
// if(err){
// console.log("error");
// } else{
// console.log(todo)
// }
// });
// new
app.get("/", function(req, res){
res.redirect("list");
});
// Index
app.get("/list", function(req, res){
Todo.find({}, function(err, todoski){
if (err){
console.log(err)
} else{
res.render("index", {todos: todoski});
}
})
});
// create
app.post("/list", function(req, res){
// bodyperser get d info from d form
Todo.create(req.body, function(err, newTodo){
console.log(req.body)
console.log(newTodo)
if(err){
res.render("index");
} else{
res.redirect("/list");
}
});
// Delete
app.delete("/list/:id/", function(req, res){
Todo.findByIdAndRemove(req.params.id, function(err){
console.log(req.params.id);
if(err){
res.redirect("/")
console.log("error!")
} else{
res.redirect("/list");
}
})
});
app.get("*", function(req, res){
res.send("Page not exist!!!")
})
// const correct = req.body.todo
// if(correct.toLowerCase() == "blue"){
// res.render("show");
// } else{
// res.render("edit")
// }
// res.send(`
// <p>congratulations!!!" </P>
// <a href="/">Back to homepage</a>
// `)
// } else {
// res.send(`
// <p>Sorry!" </P>
// <a href="/">Back to homepage</a>
// `)
// }
})
app.listen(1010, function(){
console.log("served!!!!!!")
})