-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (68 loc) · 1.88 KB
/
index.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
import { error } from "console";
import express from "express";
import mongoose from "mongoose";
import path from "path";
const app = express();
app.set("view engine", "ejs");
mongoose.connect("mongodb://127.0.0.1:27017", {
dbName: "backend",
})
.then(() => console.log("Database Connected"))
.catch((error) => console.log(error))
//Structure
const myShema = mongoose.Schema({
email: { type: String, required: true },
password: { type: String, required: true },
})
//Create a collection
const User = mongoose.model("User", myShema);
//! Middleware
app.use(express.static(path.join(path.resolve(), "public")));
app.use(express.urlencoded({ extended: true }));
// Array
const users = [];
// app.get("/home", (req, res) => {
// // res.send("Hello World!");
// const location = path.resolve();
// const fileSand = path.join(location, "./public/index.html");
// console.log(path.join(location, "./public/index.html"));
// res.sendFile(fileSand);
// });
//! For Views Folder
app.get("/", (req, res) => {
res.render("index");
});
//with this api we use for data insert with mongoose create function
app.get("/add", async (req, res) => {
await User.create({
email: "[email protected]",
password: "1255@PW"
})
console.log("Data Inserted"),
res.send("Data Inserted")
});
//
app.get("/success", (req, res) => {
res.render("success");
})
// //! For Views Folder
app.post("/contact", async (req, res) => {
const { email, password } = req.body;
await User.create({ email, password });
// console.log(`${massage} data inserted in Database`);
res.redirect("/success");
});
app.get('/users', (req, res) => {
res.json({
User,
})
})
// //* For Public Folder
// app.get("/pub", (req, res) => {
// app.use(express.static(path.join(path.resolve(), "public")));
// const location = path.resolve();
// res.sendFile("index.html");
// });
app.listen(3000, () => {
console.log("Server Working");
});