-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·112 lines (96 loc) · 2.86 KB
/
server.js
File metadata and controls
executable file
·112 lines (96 loc) · 2.86 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const mongoose = require('mongoose');
const fs = require('fs')
// The line below contains the password to MongoDB Atlas and is therefore unsafe.
// Maintainers of this website should consider removing it to avoid security issues.
mongoose.connect("mongodb+srv://shuibiosolution:shuibiosolution@cluster0.b04va.mongodb.net/messageDB?retryWrites=true&w=majority", { useNewUrlParser: true });
const messageSchema = new mongoose.Schema({
name: {
type: String
},
email: {
type: String
},
subject: {
type: String
},
message: {
type: String
},
time: {
type: Date,
default: Date.now
}
})
const Message = mongoose.model("Message", messageSchema);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public/images'));
app.use(express.static(__dirname + '/build'));
app.get('/', (req, res) => {
// read in the index.html file
fs.readFile(__dirname + "/build/index.min.html", 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
// replace the special strings with server generated strings
result = data.replace(/\$OG_IMAGE/g, 'https://live.staticflickr.com/65535/51341729015_5e40d93442_k.jpg');
res.send(result);
});
})
app.get('/water', (req, res) => {
res.sendFile(__dirname + "/build/water.min.html");
})
app.get('/earth', (req, res) => {
res.sendFile(__dirname + "/build/earth.min.html");
})
app.get('/about', (req, res) => {
res.sendFile(__dirname + "/build/about.min.html");
})
app.get('/en', (req, res) => {
res.sendFile(__dirname + "/build/index.en.min.html");
})
app.get('/earth.en', (req, res) => {
res.sendFile(__dirname + "/build/earth.en.min.html");
})
app.get('/water.en', (req, res) => {
res.sendFile(__dirname + "/build/water.en.min.html");
})
app.get('/about.en', (req, res) => {
res.sendFile(__dirname + "/build/about.en.min.html");
})
app.post("/contact", (req, res) => {
console.log(req.body);
let status = true;
if (!messageValidation(req.body)) {
console.log("Input data is illegal.");
status = false;
}
else {
Message.create({
name: req.body.name,
email: req.body.email,
subject: req.body.subject,
message: req.body.message
}, (error) => {
if (error) {
status = false;
console.log(error);
}
});
}
res.send(status);
})
function messageValidation(message) {
for (item in message) {
if (message[item] === '') {
return false;
}
}
return true;
}
app.listen(port, () => {
console.log(`Web app listening at http://localhost:${port}`)
})