-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (78 loc) · 2.55 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require("express");
const path = require("path");
const fs = require("fs");
const app = express();
const mongoose = require('mongoose');
// const bodyparser = require('body-parser');
const port = 80;
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/portfoliooo');
// use `await mongoose.connect('mongodb://user:password@127.0.0.1:27017/test');` if your database has auth enabled
}
const danceapps = new mongoose.Schema({
'name': String,
'phone': String,
'email': String,
'desc': String
});
const Contact = mongoose.model('portfoliooo', danceapps);
// 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', 'ejs') // 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('index.html', params);
})
app.get("/contact", (req, res)=>{
const params = { }
res.status(200).render('contact.html', 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 databse')
})
})
// START THE SERVER
app.listen(port, ()=>{
console.log(`The application started successfully on port ${port}`);
});
// var express = require("express");
// var app = express();
// var port = 3000;
// app.get("/", (req, res) => {
// res.sendFile(__dirname + "/contact.html");
// });
// var mongoose = require("mongoose");
// mongoose.Promise = global.Promise;
// mongoose.connect("mongodb://localhost:27017/node-demo");
// var nameSchema = new mongoose.Schema({
// name: String,
// email: String,
// phone:String,
// reason:String
// });
// var User = mongoose.model("User", nameSchema);
// var bodyParser = require('body-parser');
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// app.post("/addname", (req, res) => {
// var myData = new User(req.body);
// myData.save()
// .then(item => {
// res.send("item saved to database");
// })
// .catch(err => {
// res.status(400).send("unable to save to database");
// });
// });
// app.listen(port, () => {
// console.log("Server listening on port " + port);
// });