-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·57 lines (48 loc) · 1.55 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
const express = require('express');
const connectDB = require('./config/db');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const expressValidator = require('express-validator');
require('dotenv').config();
const path = require('path');
// Import routes
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
const categoryRoutes = require('./routes/category');
const postRoutes = require('./routes/post');
const commentRoutes = require('./routes/comment');
const replyRoutes = require('./routes/reply');
// Express
const app = express();
// Db
connectDB();
// Middlewares
app.use(morgan('dev'));
// TODO - is bodyparser needed?
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: true }));
// TODO - Is it needed?
// app.use(cookieParser());
app.use(express.json({ extended: false }));
app.use(expressValidator());
app.use(cors());
app.use('/api', authRoutes);
app.use('/api', userRoutes);
app.use('/api', categoryRoutes);
app.use('/api', postRoutes);
app.use('/api', commentRoutes);
app.use('/api', replyRoutes);
// Serve static assets in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`Server is running on ${port}`);
});