-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (57 loc) · 1.91 KB
/
index.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
70
71
72
73
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const cors = require("cors");
const dotenv = require("dotenv");
const webpack = require("webpack");
const sessions = require("./routes/session");
const auth = require("./routes/auth");
const api = require("./routes/api/index");
const router = require("./routes");
const { isAuthenticated } = require("./controllers/auth");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackDevConfig = require("./webpack.dev.js");
const compiler = webpack(webpackDevConfig);
dotenv.config();
// SERVER
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser(process.env.SESSION_SECRET));
app.use(cors({
credentials: true,
origin: true
}));
app.use(sessions);
app.use(auth);
app.use('/api', api);
// Static assets such as login.css
// and index.bundle.js (the React app)
app.use(express.static("login"));
app.get('/login', (req, res) => {
res.sendFile(path.resolve(__dirname, "./login/login.html"))
});
app.use(isAuthenticated);
app.use(express.static("public"));
if (process.env.NODE_ENV !== "production") {
app.use(
webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackDevConfig.output.publicPath
})
);
app.use(require("webpack-hot-middleware")(compiler));
}
// main route
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, "./public/index.html"))
});
// This loads both the HTML file that renders
// the actual React app and the login HTML file
// We MUST place it last or else when the browser
// makes a request to /login.css or /index.bundle.js
// it will get swallowed up by the React app instead
// of the server
const PORT = parseInt(process.env.PORT, 10) || 3000;
app.listen(PORT, () => console.log(`App listening on port ${PORT}!`));