-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (46 loc) · 1.18 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
import express from "express"
import path from "path"
import mongoose from "mongoose"
const app= express()
// Mongodb connection
mongoose.connect("mongodb://localhost:27017",{
dbName:"Backend"
}).then(()=>console.log("database is connecting")).catch((e)=>console.log(e))
// schema form data
const userSchema= new mongoose.Schema({
name : String,
email: String,
})
// Collection
const data =mongoose.model("userdata",userSchema)
//Middlewares
app.set("view engine","ejs")
app.use(express.static(path.join(path.resolve(),"public")))
app.use(express.urlencoded({extended: true}))
app.get("/",(req, res) => {
res.render("index")
})
app.get("/add",async(req,res)=>
{
await data.create({name: "azeem",email:"[email protected]"})
res.send("nice")
})
app.post("/contact", async(req,res) =>
{
const message ={name : req.body.name , email: req.body.email}
await data.create(message)
res.redirect("success")
})
app.get("/success",(req,res) =>
{
res.render("success")
})
app.get("/user",(req,res) =>
{
res.json({
user
})
})
app.listen(4000,() =>{
console.log("server is running")
})