-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (48 loc) · 1.6 KB
/
server.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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const Feedback = require("./models/fb");
const app = express();
const port = 3000;
mongoose.connect("mongodb://localhost:27017/coder_one_project", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDb Connected"))
.catch((err) => console.error("MongoDb connection error", err));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("view"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "./view/index.html");
});
app.post("/submit-feedback", async (req, res) => {
const { name, contactNumber, email, feedback: feedbackText } = req.body;
const feedback = new Feedback({
name,
contactNumber,
email,
feedback: feedbackText,
});
try {
await feedback.save();
console.log("FeedBack Saved Successfully😊!!");
res.send(`
<html>
<head>
<title>Feedback Submitted 📩</title>
</head>
<body>
<h1>Thank You 🤗!!</h1>
<p>Your feedback has been sucessfully submitted 🙌</p>
<a href="/">Go Back to Form</a>
</body>
</html>
`);
} catch (err) {
console.err("Error Saving feedback:", err);
res.status(500).send("There was an error in submitting your feedback");
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});