-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (42 loc) · 1.37 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
const express = require("express");
const path = require("path");
const mongoose = require('mongoose');
const cors = require("cors");
const config = require("./config");
const authController = require("./controllers/auth.controller");
const plantController = require("./controllers/plant.controller");
const app = express();
const expressSession = require("express-session"); // Express library to handle sessions
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
expressSession({
secret: process.env.EXPRESS_SESSION_SECRET_KEY, // Reads the secret key
})
);
app.use(authController);
app.use(plantController);
//Link front-end to back-end
app.use(express.static("./leafy-client/build"));
app.get('*', function(req, res) {
res.sendFile('index.html', {root: path.join(__dirname, './leafy-client/build/')});
});
const connectionParams={
useNewUrlParser: true,
useUnifiedTopology: true
}
mongoose.connect(config.dbConnectionString,connectionParams)
.then( () => {
console.log("Connected to database ");
})
.catch( (err) => {
console.error(`Error connecting to the database. \n${err}`);
})
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});