-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (55 loc) · 1.61 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
//Importing Libraries
const express=require('express');
const path=require('path');
const app=express();
const bodyparser=require('body-parser');
const mongoose = require('mongoose');
const uri=process.env.MONGODB_URI || 'mongodb://localhost:27017/contactbgmi'
main().catch(err => console.log(err));
async function main() {
await mongoose.connect(uri,{useNewUrlParser:true,useUnifiedTopology:true});
}
//defining schema
const contactSchema = new mongoose.Schema({
name: String,
gamename: String,
phone: String,
bgmiid: String,
desc: String,
});
//Model
const Contact = mongoose.model('Contact', contactSchema);
//server port
const port=process.env.PORT || 8000;
//EXPRESS SPECIFIC STUFFS
app.use('/static',express.static('static'))//for serving static files
app.use(express.urlencoded())
//PUG SPECIFIC STUFFS
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.render('home.pug');
})
app.get('/about',(req,res)=>{
res.render('about.pug');
})
app.get('/services',(req,res)=>{
res.render('services.pug');
})
app.get('/contact',(req,res)=>{
res.render('contact.pug');
})
app.post('/contact',(req,res)=>{
var myData=new Contact(req.body);
myData.save().then(()=>{
res.send("Your data has been safely saved to yash's Database")
}).catch(()=>{
res.status(400).send("Item was not saved to the database")
});
})
//START THE SERVER
app.listen(port,()=>{
console.log(`the application started successfully on port ${port}`)
});