-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (47 loc) · 1.59 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
const express = require('express');
const mongo = require('./config/mongo');
const routes = require('./routes/routes');
const authRoutes = require('./routes/auth');
const protectedRoute = require('./routes/protectedRoute');
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = require('./config/swaggerConfig');
const fs = require('fs');
const morgan = require('morgan');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 80;
const accessLogStream = fs.createWriteStream('access.log', { flags: 'a' });
const cors = require('cors');
const http = require('http');
function errorHandler(error, req, res, next) {
res.status(error.status || 500);
res.send({
error: {
message: error.message
}
});
}
const uploadDir = 'uploads/images';
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
// Serve the static files from the React app
app.use(express.static('frontend/build'));
app.use(express.static('frontend/public'));
app.use(express.static('uploads'));
app.use(express.json());
app.use(cors());
app.use(morgan('combined', { stream: accessLogStream }));
app.use(errorHandler);
mongo();
app.use('/api', routes);
app.use('/auth', authRoutes);
app.use('/protected', protectedRoute);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Handles any requests that don't match the ones above
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/frontend/build/index.html'));
});
http.createServer({}, app).listen(PORT, () => {
console.log(`OpenSpace-Hub listening on port ${PORT}`);
});