-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
50 lines (40 loc) · 1.34 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
'use strict';
const express = require('express');
const path = require('path');
const connectDB = require('./config/db');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const { getNotifications } = require('./services/notifications');
const setup = async () => {
if (process.env.NODE_ENV !== 'test') {
// Connect database.
await connectDB();
console.log('After connecting database...');
}
// Init middleware.
app.use(express.json({ extended: false }));
// Define routes.
const API = 'api/v1';
app.use(`/${API}/auth`, require(`./${API}/routes/auth`));
app.use(`/${API}/users`, require(`./${API}/routes/user`));
app.use(`/${API}/notifications`, require(`./${API}/routes/notification`));
app.use(`/${API}/restaurants`, require(`./${API}/routes/restaurant`));
// 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'))
);
}
// Listening to app.
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
if (process.env.NODE_ENV !== 'test') {
getNotifications();
}
};
setup();
// Export app for testing purposes.
module.exports = app;