-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (51 loc) · 1.5 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
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const express = require("express");
const app = express();
require("./config/passport-setup");
const path = require("path");
const cors = require("cors");
const cookieSession = require("cookie-session");
const passport = require("passport");
// database
require("./database")();
app.use(
"/public/posts/images",
express.static(__dirname + "/public/posts/images")
);
app.use(express.json());
app.use(
cors({
origin: "http://localhost:3000", // allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true // allow session cookie from browser to pass through
})
);
app.use(
cookieSession({
maxAge: 24 * 3600 * 1000, //1 day
keys: [process.env.SESSION_KEY]
})
);
app.use(passport.initialize());
app.use(passport.session());
//routes
const userRouter = require("./routes/user");
app.use("/api/users", userRouter);
const postRouter = require("./routes/post");
app.use("/api/posts", postRouter);
const authRouter = require("./routes/auth");
app.use("/api/auth", authRouter);
if (
process.env.NODE_ENV === undefined ||
process.env.NODE_ENV === "production"
) {
console.log(process.env.NODE_ENV);
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () =>
console.log(`server running on port ${PORT}`)
);