-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (43 loc) · 1.54 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
const express = require('express');
require('dotenv').config({ path: './config/.env' });
const connectDB = require('./db');
const cors = require('cors');
const path = require('path');
const cookieParser = require('cookie-parser');
const {
auth,
protectAPI,
protectApp,
isAlreadyAuthenticated,
} = require('./middleware/auth');
const app = express();
// Connect database
connectDB();
//Init middleware
app.use(cors());
app.use(express.json({ extended: false }));
app.use(cookieParser());
// app.get('/', (req, res) => res.send('API Running'));
// Check if logged in
app.use(auth);
// Define Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/auth', require('./routes/api/auth'));
app.use('/api/students', protectAPI, require('./routes/api/students'));
app.use('/api/fees', protectAPI, require('./routes/api/fees'));
app.use('/api/masters', protectAPI, require('./routes/api/masters'));
// Serve react app static assets
// Set static folder
app.use('/app', protectApp, express.static('client/build'));
app.get('/app/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
app.use('/', isAlreadyAuthenticated, express.static('static'));
app.get('/', (req, res) =>
res.sendFile(path.join(__dirname, 'static', 'index.html'))
);
app.get('/login', (req, res) =>
res.sendFile(path.join(__dirname, 'static', 'login.html'))
);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log('Server up and running...'));