forked from Sushant2/Octocat-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (50 loc) · 1.42 KB
/
index.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
var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var app = express();
//mongoose connection
mongoose.connect("mongodb://localhost/todo", { useNewUrlParser: true , useUnifiedTopology: true})
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
//mongoose schema
var todoSchema = new mongoose.Schema({
name: String
});
var Todo = mongoose.model("Todo", todoSchema);
// var todoList = [
// "wash the casr & change oil",
// "Do laundry",
// "Buy groceries & make dinner"
// ]
// Express routes here
//default route
app.get("/",(req, res)=>{
Todo.find({}, (err, todoList)=>{
if(err)
console.log(err);
else
res.render("index.ejs", {todoList: todoList});
})
});
// //submit button route
// app.post('/newtodo', (req, res)=>{
// console.log("item submitted");
// var newItem = new Todo({
// name = req.body.item
// });
// Todo.create(newItem, (err, Todo)=>{
// if(err)
// console.log(err);
// else
// console.log("Inserted item:"+newItem);
// })
// res.redirect("/");
// });
//catch all other routes
app.get("*", (req, res)=>{
res.send("<h1>Invalid Page</h1>")
});
// server listening
app.listen(3000, ()=>{
console.log("Server started on port 3000");
})