-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (80 loc) · 2.61 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
const express = require('express')
const app = express();
const path = require("path");
const mongoose = require("mongoose");
const Organization = require("./models/organization.js");
const MONGO_URL = "mongodb://127.0.0.1:27017/donorData";
mongoose.connect(MONGO_URL)
.then(() => console.log("Connected to MongoDB"))
.catch(err => console.error("Database connection error:", err));
app.use(express.urlencoded({ extended: true }));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.get('/',(req, res) =>{
res.render("home.ejs");
});
app.get("/about",(req,res)=>{
res.render("about.ejs");
});
app.get("/service", (req,res)=>{
res.render("service.ejs");
});
app.get("/contact", (req,res)=>{
res.render("contact.ejs");
});
app.get("/donate", (req,res)=>{
res.render("donate.ejs");
});
app.get("/receive", async (req, res) => {
try {
const organizations = await Organization.find(); // Fetch all organizations from the database
res.render("receive", { organizations }); // Pass data to the EJS template
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/donate/register", (req,res)=>{
res.render("donate-reg.ejs");
});
app.get("/receive/register", (req,res)=>{
res.render("receive-reg.ejs");
});
app.post("/submit/donor", async (req, res) => {
try {
const data = {
organizationType: req.body.organizationType,
organizationDetails: {
orgName: req.body.orgName,
contactPerson: req.body.contactPerson,
email: req.body.email,
phone: req.body.phone,
},
locationDetails: {
address: req.body.address,
city: req.body.city,
state: req.body.state,
pincode: req.body.pincode,
capacity: req.body.capacity,
typicalTimings: req.body.typicalTimings,
},
foodAndSafety: {
foodTypes: req.body.foodTypes || [],
licenses: req.body.licenses || [],
},
accountDetails: {
password: req.body.password,
},
};
const newOrganization = new Organization(data);
await newOrganization.save();
res.status(200).redirect("/");
} catch (error) {
console.error("Error saving data:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(8080, ()=>{
console.log("Server is listening");
});