-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 2.02 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import 'dotenv/config';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import 'express-async-errors';
import express from 'express';
import cookieParser from 'cookie-parser';
import morgan from 'morgan';
import { v2 as cloudinary } from 'cloudinary';
import connectDB from './db/connect.js';
// routers
import authRouter from './routes/authRouter.js';
import jobRouter from './routes/jobRouter.js';
import userRouter from './routes/userRouter.js';
// middlewares
import notFoundMiddleware from './middleware/notFoundMiddleware.js';
import errorHandlerMiddleware from './middleware/errorHandlerMiddleware.js';
import { authenticateUser } from './middleware/authMiddleware.js';
import helmet from 'helmet';
import mongoSanitize from 'express-mongo-sanitize';
import rateLimiter from './middleware/rateLimiter.js';
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET,
});
const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// app.use(express.static(path.resolve(__dirname, './public')));
app.use(express.static(path.resolve(__dirname, './client/dist')));
app.use(cookieParser());
app.use(express.json());
app.use(helmet());
app.use(mongoSanitize());
app.get('/api/v1', (req, res) => {
res.send('<h1>Jobify API!</h1>');
});
app.use('/api/v1/jobs', [rateLimiter(), authenticateUser], jobRouter);
app.use('/api/v1/users', [rateLimiter(), authenticateUser], userRouter);
app.use('/api/v1/auth', authRouter);
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './client/dist', 'index.html'));
});
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 5000;
const startServer = async () => {
try {
await connectDB(process.env.MONGO_URL);
app.listen(port, () => console.log(`Server is running on port ${port}...`));
} catch (error) {
console.log(error);
}
};
startServer();