-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (54 loc) · 1.87 KB
/
server.js
File metadata and controls
76 lines (54 loc) · 1.87 KB
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
import express from 'express'
import path from 'path'
import { fileURLToPath } from 'url';
import cors from 'cors'
import cookieParser from 'cookie-parser';
import {config} from 'dotenv';
import mongoose from 'mongoose';
import {logger} from './middleware/logEvents.js';
import {errorHandler} from './middleware/errorHandler.js';
import {verifyJWT} from './middleware/verifyJWT.js';
import { credentials } from './middleware/credintials.js';
import corsOptions from './config/corsOptions.js';
import { connectDB } from './config/dbConn.js';
import root from './routes/root.js'
import employees from './routes/api/employees.js';
import register from './routes/register.js';
import auth from './routes/auth.js';
import refresh from './routes/refresh.js';
import logout from './routes/logout.js';
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express();
const PORT = process.env.PORT || 3500;
config();
// Connect to mongoDB
connectDB();
app.use(logger);
app.use(credentials);
app.use(cors(corsOptions));
app.use(express.urlencoded({extended:false}));
app.use(express.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'/public')));
app.use('/',root);
app.use('/auth',auth);
app.use('/refresh',refresh);
app.use('/register',register);
app.use('/logout',logout);
app.use(verifyJWT); // before acess this endpoint verify this , above this line will not verfied.
app.use('/employees',employees);
app.get(/\/*/,(req,res)=>{
res.status(404);
if(req.accepts('html')){
res.sendFile(path.join(__dirname,'views','404.html'));
}
else if(req.accepts('json')){
res.json({error:'404 not found'});
}
})
app.use(errorHandler);
mongoose.connection.once('open',()=>{
console.log("Connected To MongoDB");
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
});