-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (53 loc) · 1.66 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
const express=require("express")
const path=require("path");
const app=express();
const mongoose = require("mongoose");
const bodyparser=require("body-parser");
//database connection
mongoose
.connect("mongodb://localhost/contactDance", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("Failed to connect to MongoDB", err));
const port=8000;
//defind mongoose schema
const contactSchema = new mongoose.Schema({
name: String,
phone: String,
email: String,
address: String,
desc: String
});
const Contact = mongoose.model('Contact',contactSchema);
//Express Specific stuff
app.use('/static',express.static('static'))//for serving static file
app.use(express.urlencoded())
// app.use(bodyParser.urlencoded({ extended: true })); // Parse URL-encoded bodies
//pug specific stuff
app.set('view engine', 'pug')//set the template engine as pug
app.set('views',path.join(__dirname,'views'))//set the views directory
//endpoints
app.get('/',(req,res)=>{
const params={}
res.status(200).render('home.pug',params);
})
app.get('/contact',(req,res)=>{
const params={}
res.status(200).render('contact.pug',params);
})
app.post('/contact',(req,res)=>{
// const params={}
var myData=new Contact(req.body);
myData.save().then(()=>{
res.send("this items has been saved to the database")
}).catch(()=>{
res.status(400).send("Item was not saved to the database")
});
// res.status(200).render('contact.pug');
});
//start the server
app.listen(port,()=>{
console.log(`The application started successfully on port ${port}`)
})