-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (40 loc) · 1.14 KB
/
server.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
// dendencies
const express = require("express");
const logger = require("morgan");
const mongoose = require("mongoose");
const routes = require("./routes");
const Message = require("./messageModel.js");
// setup the port and the express app
const PORT = process.env.PORT || 4000;
const app = express();
app.use(logger("dev"));
// setup the mongodb database
mongoose.connect(
process.env.MONGODB_URI || "mongodb+srv://Jonayah:seMDz^^[email protected]/thweb?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
// middlewares for accepting post requests
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// routes
app.post("/contact/submit", ({ body }, res) => {
const message = new Message(body);
Message.create(message)
.then(dbMessage => {
res.json(dbMessage);
})
.catch(err => {
res.json(err);
});
});
// start the server
app.listen(PORT, () => {
console.log(`You're being served on port ${PORT}!`);
});