-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongoose.js
More file actions
96 lines (68 loc) · 1.78 KB
/
mongoose.js
File metadata and controls
96 lines (68 loc) · 1.78 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
const mongoose = require('mongoose')
const path = require("path");
const express = require("express");
var router = express.Router();
const app = express();
publicDirectoryPath = path.join(__dirname, "public");
app.use(express.static(publicDirectoryPath));
app.set("view engine", "hbs");
const viewsPath = path.join(__dirname, "views");
app.set("views", viewsPath);
mongoose.connect('mongodb://127.0.0.1:27017/task-manager-api', {
useUnifiedTopology: true,
useCreateIndex: true
})
const User = mongoose.model('User', {
name: {
type: String,
required: true
},
age:{
type: Number
}
})
const Tasks = mongoose.model('Tasks', {
Description:{
type:String,
},
completed:{
type: Boolean
}
})
app.get('/',(req,res)=>{
User.find((err,docs)=>{
if(!err)
{
console.log(docs);
res.render('index',{
list:docs,
title: 'Fetch and display data from DataBase in Html'
})
}else{
console.log(err);
}
})
})
// const t = new Tasks({
// Description: 'prepare for test',
// completed: false
// })
// t.save().then(()=>{
// console.log(t)
// }).catch(()=>{
// console.log('Error!', error)
// })
//create an instance of model
// const me = new User({
// //name:'shehzan',
// age: 2
// })
// //save to database
// me.save().then(()=>{
// console.log(me);
// }).catch((error)=>{
// console.log(error);
// })
app.listen(3000, () => {
console.log("Server is Up on Port 3000");
});