-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
57 lines (41 loc) · 1.41 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
import express from 'express';
import mongoose from 'mongoose';
import router from './routes/api.js';
import rateLimit from 'express-rate-limit';
import * as path from "path";
import helmet from 'helmet';
import hpp from 'hpp';
import cors from 'cors';
import cookieParser from "cookie-parser";
import {MAX_JSON_SIZE, MONGODB_CONNECTION, PORT, REQUEST_LIMIT_NUMBER, REQUEST_LIMIT_TIME, URL_ENCODED, WEB_CACHE} from "./app/config/config.js";
const app = express();
// middlewares
app.use(cors());
app.use(helmet());
app.use(hpp());
app.use(cookieParser())
app.use(express.json({ limit: MAX_JSON_SIZE }));
app.use(express.urlencoded({extended: URL_ENCODED}));
const limiter = rateLimit({ windowMs: REQUEST_LIMIT_TIME, max: REQUEST_LIMIT_NUMBER });
app.use(limiter);
// Web cache validation and conditional requests in Http
app.set('etag', WEB_CACHE);
// MongoDB connection
mongoose.connect(MONGODB_CONNECTION, {autoIndex: true})
.then(() => {
console.log("Database Connected");
})
.catch((err) => {
console.error("Database connection error:", err);
});
// API routes
app.use("/api", router);
// Serve static assets for React front end
app.use(express.static('dist'));
// Serve React front end for all routes not handled by the API
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
});
app.listen(PORT,()=>{
console.log(`MERN-X Server Running ${PORT}`)
});