-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
83 lines (74 loc) · 2.86 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import dotenv from 'dotenv';
import multer from 'multer';
import validator from 'express-validator';
import cors from 'cors';
import bodyParser from 'body-parser';
import swaggerUi from 'swagger-ui-express';
import userRoutes from './server/routes/user';
import businessRoutes from './server/routes/businesses';
import adminRoutes from './server/routes/admin';
import swaggerDocument from './swagger.json';
import config from './webpack.config';
import prodConfig from './webpack.config.prod';
import customValidations from './server/validations/customValidations';
dotenv.config();
const app = express();
const compiler = webpack(config);
// EXPRESS MIDDLEWARES
app.use('/images', express.static(path.join(__dirname, './client/public/images')));
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
if (process.env.NODE_ENV !== 'production') {
app.use(webpackDevMiddleware(compiler));
app.use(webpackHotMiddleware(compiler));
} else {
app.use(webpackDevMiddleware(webpack(prodConfig)));
}
app.use(cors({ credentials: true, origin: true }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(validator({
customValidators: customValidations
}));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
next();
});
// ROUTES
app.get('/api', (req, res) => {
const apiRootMessage = {
message: 'Welcome to the WEconnect API, Available endpoints are shown below',
endpoints: {
registerBusiness: 'POST /api/v1/businesses/',
getBusinesses: 'GET /api/v1/businesses/',
getBusiness: 'GET /api/v1/businesses/:businessId',
updateBusiness: 'PUT /api/v1/businesses/:businessId',
deleteBusiness: 'DELETE /api/v1/businesses/:businessId',
getBusinessReview: 'GET /api/v1/businesses/:businessId/reviews',
addBusinessReview: 'POST /api/v1/businesses/:businessId/review'
}
};
res.status(200).json(apiRootMessage);
});
app.use('/api/v1/auth', userRoutes);
app.use('/api/v1/businesses', businessRoutes);
app.use('/api/v1/admin', adminRoutes);
// CATCH ALL ENDPOINT THAT DO NOT EXIST AND RETURN ERROR MESSAGE
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, './client/index.html'));
});
app.all('*', (req, res) => {
res.status(404).json({ message: 'Endpoint not Found' });
});
// LISTEN TO ACTIVITY ON PORT
const port = parseInt(process.env.PORT, 10) || 8080;
app.listen(port);
export default app;