-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
119 lines (102 loc) · 3.01 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
const express = require("express");
const bodyParser = require("body-parser");
// const datepicker = require("js-datepicker");
const app = express();
app.use(express.urlencoded({ extented: true }));
const { MongoClient } = require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const url =
"mongodb+srv://anupamAdmin:anupamAdmin@cluster0.2c2zj.mongodb.net/student_db";
const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.get("/", (req, res) => {
res.render("student.ejs");
});
app.get("/records", (req, res) => {
const database = client.db("student_db");
const students = database.collection("student_records");
students.find({}).toArray(function (err, std_record) {
// console.log(std_record);
res.render("records", {
records: std_record,
});
});
});
app.get("/Student", function (req, res) {
res.redirect("/");
});
app.get("/Admin", function (req, res) {
res.render("Admin.ejs");
});
app.get("/cal", function (req, res) {
res.render("cal.ejs");
});
app.get("/stdTable", (req, res) => {
// res.render("stdTable");
const database = client.db("student_db");
const students = database.collection("student_records");
students.find({}).toArray(function (err, std_record) {
// console.log(std_record);
res.render("stdTable", {
records: std_record,
});
});
});
app.post("/", function (req, res) {
let roll = req.body.rollno;
const database = client.db("student_db");
const students = database.collection("student_records");
students.find({}).toArray(function (err, std_record) {
res.render("stdTable", {
records: std_record,
rollno: roll,
basis: 1,
});
});
});
app.post("/cal", function (req, res) {
console.log(req.body);
// let name = req.body.stdName;
let date = req.body.date;
const database = client.db("student_db");
const students = database.collection("student_records");
students.find({}).toArray(function (err, std_record) {
res.render("stdTable", {
records: std_record,
date: date,
basis: 2,
});
});
});
// app.get("/Student", function (req, res) {
// res.redirect("/")
// });
function fetchData() {
const database = client.db("student_db");
const students = database.collection("student_records");
students.find({}).toArray(function (err, std_record) {
// console.log(std_record);
return std_record;
});
}
async function run() {
try {
await client.connect();
console.log("MongoDB connected");
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
run().catch(console.dir);
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, () => {
console.log("App listening at http://localhost:" + port);
});