-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (52 loc) · 1.79 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
59
60
61
62
63
64
65
66
67
68
69
// mongorestore --uri "mongodb+srv://ramtin:[email protected]/"
const path = require("path");
const express = require("express");
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv').config()
const app = express();
const PORT = process.env.PORT;
const MONGO_URI = process.env.MONGO_PROD_URI || process.env.MONGO_URI;
mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: "regal"
})
.then(() => console.log('Connected to Mongo DB.'))
.catch(err => console.log(err));
mongoose.connection.on('connected', function () {
process.env.NODE_ENV === 'production'
?
console.log(`Mongoose default connection open to ${process.env.MONGO_PROD_URI}`)
:
console.log(`Mongoose default connection open to ${MONGO_URI}`);
});
mongoose.connection.on('error',function (err) {
console.log(`Mongoose default connection error: ${err}`);
});
mongoose.connection.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
});
if (process.env.NODE_ENV === "development") {
app.use(cors());
}
//Express v4.16.0 and higher now include middleware functionality. Body Parser is now deprecated for this use.
app.use(express.urlencoded({
extended: true
}));
app.use(express.json());
app.use(express.static(path.resolve(__dirname, 'client')));
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
const routes = require('./routes');
app.use(routes);
// app.use((req, res, next) => {
// res.status(404).json({ message: "Route not found" });
// })
app.get("*", function(req, res) {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
app.listen(PORT, ()=> {
console.log(`🌎 ==> Server now on port ${PORT}!`);
})