-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (53 loc) · 1.37 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
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var autoIncrement = require("mongodb-autoincrement");
//var MO = require("method-override");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
mongoose.connect("mongodb://localhost/todo_db");
app.set("view engine","ejs");
app.use(express.static("public"));
//app.use(MO("_method"));
var todoSchema = new mongoose.Schema
({
Title: String,
IsDone: Boolean,
UpdatedAt: String
});
var todo = mongoose.model("todo",todoSchema);
/*todo.create({
Title: "first todo",
IsDone: true,
UpdatedAt: "06/08/2018"
});*/
//var p = todo.find();
app.get("/apis/todoes",function(req,res){
todo.find({},function(err,todos){
if(err)
{console.log("err");}
else
{
res.send(JSON.stringify(todos));
}
});
});
app.post("/apis/todoes",function(req,res){
//console.log(req.body);
todo.create(req.body, function(err,newtodo){
if(err)
{console.log("err");}
else{
console.log("added");
}
});
});
app.delete("/apis/todoes/:id",function(req,res){
todo.findByIdAndRemove(req.params.id,function(err){
if(err)
{console.log(err);}
});
});
//app.put("")
app.listen(process.env.PORT || 3000);