-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (46 loc) · 1.65 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
const express = require("express");
const app = express();
var mongoose=require('mongoose');
const bodyparser=require('body-parser')
const port = process.env.PORT || 8000;
const path = require('path');
// connecting to the database:-
const url = 'mongodb+srv://technohut_admin:@[email protected]/technohut?retryWrites=true&w=majority'
mongoose.connect(url,{useNewUrlParser:true,useUnifiedTopology:true})
.then(()=>console.log('database connneceed...'))
.catch(error=>console.log(error,'database is not connected.....'));
//******creating schema******** */
// creating schema
var contactSchema=new mongoose.Schema({
firstname: String,
lastname: String,
email: String,
subject: String,
message: String
});
var Contact=mongoose.model('Contact',contactSchema);
// ******express related config**************
app.use('/static', express.static(path.join(__dirname, 'static')));
app.use(express.urlencoded());
// ********** Setting get endpoints***************
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/about', (req, res) => {
res.sendFile(__dirname + '/about.html');
});
app.get('/contact', (req, res) => {
res.sendFile(__dirname + '/contact.html');
});
app.post('/contact', (req, res) => {
var myData=new Contact(req.body);
myData.save().then(()=>{
res.send("Your details has been saved.")
}).catch(()=>{
res.status(400).send("Your details has not been saved.")
})
});
// *****starting the server******
app.listen(port || process.env.port, () => {
console.log(`The application started successfully on port ${port}`);
});