-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js~
executable file
·58 lines (49 loc) · 1.42 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 path = require("path");
const mongoose = require("mongoose");
const cors = require("cors");
const bodyParser = require("body-parser");
const port = process.env.PORT || 5002;
const config = require("./settings/config");
const app = express();
// connect database
mongoose.Promise = require("bluebird");
mongoose
.connect(
config.dbUrl2,
{ useNewUrlParser: true }
)
.then(() => {
// if all is ok we will be here
console.log("Db initialized");
})
.catch(err => {
// if error we will be here
console.error("DB starting error");
//process.exit(1);
});
// parse request bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// allow cross origin requests
app.use(
cors({
exposedHeaders: "*"
})
);
// Serve static files from the React app after npm run build
app.use(express.static(path.join(__dirname, "client/build")));
app.use("/public", express.static(__dirname + "/public"));
app.use(express.static(process.cwd() + "/public"));
// serving routes
const mainRouter = require("./routes/main");
app.use("/main", mainRouter);
/*app.get('/', (req, res) => {
res.send('express server running');
});*/
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build", "index.html"));
});
// run server
app.listen(port);
console.log(`NDC English Carnival listening on ${port}`);