-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (68 loc) · 2.22 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//* middleware that helps handle errors that occur within asynchronous functions
import 'express-async-errors';
import * as dotenv from 'dotenv';
dotenv.config();
import express from 'express';
const app = express();
import mongoose from 'mongoose';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import path from 'path';
import cloudinary from 'cloudinary';
import helmet from 'helmet';
import expressMongoSanitize from 'express-mongo-sanitize';
import errorHandlerMiddleware from './middleware/errorHandlerMiddleware.js';
import { authenticateUser } from './middleware/authMiddleware.js';
import jobRouter from './routes/jobRouter.js';
import authRouter from './routes/authRouter.js';
import userRouter from './routes/userRouter.js';
//! Middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use(cookieParser());
app.use(express.json());
app.use(helmet());
app.use(expressMongoSanitize());
//! Public
const __dirname = dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.resolve(__dirname, './client/dist')));
//! Cloudinary
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET,
});
//! Routes
app.get('/', (req, res) => {
res.send('Jobify Backend');
});
app.use('/api/v1/jobs', authenticateUser, jobRouter);
app.use('/api/v1/users', authenticateUser, userRouter);
app.use('/api/v1/auth', authRouter);
//> Direct users to index.html
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './client/dist', 'index.html'));
});
//> Not Found Route (gets triggered for routes that do not exist)
app.use('*', (req, res) => {
res.status(404).json({
msg: 'not found',
});
});
//> Error Middleware (gets triggered by errors in our existing routes)
app.use(errorHandlerMiddleware);
//! Start Server
const PORT = process.env.PORT || 8000;
try {
await mongoose.connect(process.env.MONGO_URL);
console.log('DB Is Connected !!!');
app.listen(PORT, () => {
console.log('Server Running On Port ::', PORT);
});
} catch (error) {
console.log(error);
process.exit(1);
}