forked from smit-1999/Travellite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (55 loc) · 1.84 KB
/
Copy pathserver.js
File metadata and controls
60 lines (55 loc) · 1.84 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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const bodyParser = require("body-parser");
const user = require("./routes/user.controller");
const post = require("./routes/post.controller");
const notification = require("./routes/notification.controller");
const path = require("path");
const app = express();
// app.use(cors({origin: true, credentials: true}));
//DB config
const uri = require("./config/keys").MONGO_URI;
app.use(cors());
app.use(bodyParser.json());
//connect to mongo
// mongoose.connect(uri, { useNewUrlParser : true, useUnifiedTopology: true}).then(() => console.log('Mongoose connected'))
// .catch(err => console.log(err));
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});
const connection = mongoose.connection;
mongoose.Promise = global.Promise;
connection.once("open", () => {
console.log("Mongoose connected");
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//use routes
app.use("/user", user);
app.use("/post", post);
app.use("/notif", notification);
// Enable CORS
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header(
// "Access-Control-Allow-Methods",
// "GET,HEAD,OPTIONS,POST,PUT,DELETE"
// );
// res.header(
// "Access-Control-Allow-Headers",
// "Origin, X-Requested-With, Content-Type, Accept, Authorization"
// );
// next();
// });
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.resolve(__dirname, "client", "build")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const port = process.env.PORT || 4000;
//const host = process.env.YOUR_HOST || "0.0.0.0";
app.listen(port, () => console.log("Server started on port " + port));