-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (53 loc) · 1.7 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
54
55
56
57
58
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
app.use(cors());
app.use(express.json());
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
next();
});
// support parsing of application/json type post data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// create mongodb database - on cloud Amazon server(MongodB atlas)
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
// check connection to data base
const connection = mongoose.connection;
connection.once("open", () => {
console.log("Succesfully connected to Data base ");
});
// router to scores
const scoresRouter = require("./routes/scores");
//use routes from scores
app.use("/scores", scoresRouter);
app.get("/", (req, res) => {
res.send("Wpwww");
});
/***********Configure front end********* */
// "heroku-postbuild" : "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
// if in production
// if (process.env.NODE_ENV === "production") {
// //serve up static files
// app.use(express.static("client/build"));
// // connect index.html
// app.get("*", (req, res) => {
// res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
// });
// }
// listen om port 5000
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server is now running on port: ${port}`);
});