-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (73 loc) · 2.93 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require("express");
const path = require("path");
const app = express();//call the express fun and puts new express application inside the app var jt like you are creating an object of a class
const mongoose = require('mongoose');//node.js based object data modeling library use for mapping realation into mongod
const bodyparser = require("body-parser"); //use for context of post,put,patch http request where the information you want is contained in the body i.e allows you to access req.
//mongoose.connect('mongodb://localhost/contactDance', {useNewUrlParser: true});
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/contactDance');
}
const port = 8000;
// Define mongoose schema
var contactSchema = new mongoose.Schema({
name: String,
phone: String,
email: String,
address: String,
desc: String
});
var Contact = mongoose.model('Contact', contactSchema);
// EXPRESS SPECIFIC STUFF
app.use('/static', express.static('static')) // For serving static files
app.use(express.urlencoded({
extended:true
}))
// PUG SPECIFIC STUFF
app.set('view engine', 'pug') // Set the template view engine as pug where view engine as key
app.set('views', path.join(__dirname, 'views')) // Set the views directory where path is build-in node.js module used for handling file paths, and __dirname is special node.js variable that represents the absolute path of directory that contains the currently executing script.
// ENDPOINTS
app.get('/', (req, res)=>{ //'/' url path for the route
const params = { }
res.status(200).render('home.pug', params);
})
app.get('/contact', (req, res)=>{ //call back function which represent the http request,res
const params = { }
res.status(200).render('contact.pug', params);
})
app.post('/contact', (req, res)=>{
var myData = new Contact(req.body);
myData.save().then(()=>{
res.send("This item has been saved to the database")
}).catch(()=>{
res.status(400).send("Item was not saved to the database")
});
// res.status(200).render('contact.pug');
})
// Get a specific contact
//Update contact
// app.get('/update', (req, res) => {
// const params = {};
// res.status(200).render('update.pug', params);
// });
// app.post('/update', (req, res) => {
// const id = req.body.id;
// const newData = {
// name: req.body.name,
// phone: req.body.phone,
// email: req.body.email,
// address: req.body.address,
// desc: req.body.desc
// };
// Contact.findByIdAndUpdate(id, newData, { new: true })
// .then((contact) => {
// res.send(`Contact ${id} has been updated successfully`);
// })
// .catch((err) => {
// res.status(400).send(`Error updating contact ${id}: ${err.message}`);
// });
// });
// START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
});