-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (52 loc) · 1.76 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
const express = require('express');
require('dotenv').config();
require('express-async-errors');
const path = require('path');
const app = express();
// import security middleware
const cors = require('cors');
const helmet = require('helmet');
const xss = require('xss-clean');
const mongoSanitize = require('express-mongo-sanitize');
const rateLimiter = require('express-rate-limit');
// import middlewware
const cookieParser = require('cookie-parser');
const connectDB = require('./Connect/connectDB');
// import Routers
const authRouter = require('./Routes/authRouter');
const userRouter = require('./Routes/userRouter');
const jobsRouter = require('./Routes/jobRouter');
// import other middleware
const NotFoundMiddleware = require('./Middleware/NotFoundMiddleware');
const errorHandlerMiddleware = require('./Middleware/ErrorHandlerMiddleware');
// security
app.set('trust proxy', 1);
app.use(cors({ origin: 'https://jobius.netlify.app', credentials: true }));
app.use(helmet());
app.use(xss());
app.use(mongoSanitize());
// middleware
app.use(express.static(path.resolve(__dirname, './client/build')));
app.use(cookieParser(process.env.JWT_SECRET));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// routes
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/users', userRouter);
app.use('/api/v1/jobs', jobsRouter);
// other Middleware
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './client/build', 'index.html'));
});
app.use(NotFoundMiddleware);
app.use(errorHandlerMiddleware);
const Port = process.env.PORT || 5000;
const start = async () => {
try {
await connectDB(process.env.MONGO_URL);
app.listen(Port, console.log(`server listening port ${Port}...`));
} catch (error) {
console.log(error);
}
};
start();