-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.js
46 lines (37 loc) · 965 Bytes
/
27.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
// App using mongoose
const express = require("express");
const port = 3000;
const connectDB = require("./26.js");
const User = require("./25.js");
const app = express();
connectDB();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.set("views", "./views");
// GET route for the home page
app.get("/", (req, res) => {
res.render("index");
});
app.post("/user", async (req, res) => {
try {
// Create a new user document
const newUser = new User({
name: req.body.name,
age: req.body.age,
});
// Save the user to the database
await newUser.save();
res.send("User data saved successfully!");
} catch (err) {
console.error("Error saving user data:", err);
res.status(500).send("Error saving user data");
}
});
app.listen(port, (err) => {
if (err) {
console.log(err);
} else {
console.log(`Server is started at port no ${port}`);
}
});