This repository was archived by the owner on Jun 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (105 loc) · 2.67 KB
/
Copy pathserver.js
File metadata and controls
118 lines (105 loc) · 2.67 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
113
114
115
116
117
118
const compression = require("compression");
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const axios = require("axios");
const app = express();
const path = require("path");
const port = process.env.PORT || 3001;
const url =
process.env.DEV_ENV === "dev"
? process.env.MONGODB_URI
: process.env.PROD_MONGODB_URI;
mongoose.connect(url, { useNewUrlParser: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
app.use(compression());
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
let whiteList = ["https://startupgraveyard.herokuapp.com"];
if (process.env.DEV_ENV === "dev") {
whiteList.push("http://localhost:3000", "http://localhost:3001");
}
let corsOptions = {
origin: function(origin, callback) {
if (whiteList.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
}
};
app.use(cors());
app.use(express.static(path.resolve(__dirname, "build")));
app.listen(port, () => console.log(`Listening on port ${port}`));
const Company = require("./models/Company");
// route for front page
app.get("/api/home", (req, res) => {
Company.find()
.sort({ date: -1 })
.then(companies => {
res.json({
confirmation: "success",
responses: companies
});
})
.catch(err => {
res.json({
confirmation: "fail",
message: err.message
});
});
});
// route for individual card
app.get("/api/story/:id", (req, res) => {
const id = req.params.id;
Company.findById(id)
.then(company => {
res.json({
confirmation: "success",
response: company
});
})
.catch(err => {
res.json({
confirmation: "fail",
message: err.message
});
});
});
// route for form data -> DB
app.post("/api/addstory", cors(corsOptions), (req, res) => {
Company.create(req.body)
.then(company => {
res.json({
confirmation: "success"
});
})
.catch(err => {
res.json({
confirmation: "fail",
message: err.message
});
});
});
app.post("/api/recaptcha", cors(corsOptions), (req, res) => {
axios
.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${
process.env.RECAPTCHA_SECRETKEY
}&response=${req.body.response}`,
{}
)
.then(response => {
res.send(response.data);
})
.catch(error => console.error(error));
});
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "build", "index.html"));
});