-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (45 loc) · 1.28 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
import 'express-async-errors'
import * as dotenv from "dotenv";
dotenv.config();
import mongoose from 'mongoose';
import cookieParser from "cookie-parser";
// application
import express from 'express'
const app = express()
import morgan from 'morgan';
// middlewares
import errorHandlerMiddleware from './middleware/errorHandlerMiddleware.js';
import { authenticateUser } from "./middleware/authMiddleware.js";
// routers
import jobRouter from './routers/jobRouter.js'
import authRouter from "./routers/authRouter.js";
import userRouter from "./routers/userRouter.js";
// middleware
app.use(express.json())
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(cookieParser());
// routes
app.get("/", (req, res) => {
res.send("Hello World");
});
app.use("/api/v1/jobs",authenticateUser, jobRouter);
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/users",authenticateUser, userRouter);
// error middleware
app.use('*', (req,res)=>{
res.status(404).json({msg: 'Route not found'})
})
app.use(errorHandlerMiddleware)
//
const port = process.env.PORT || 5100;
try {
await mongoose.connect(process.env.MONGO_URL);
app.listen(port, () => {
console.log(`server running on PORT ${port}....`);
});
} catch (error) {
console.log(error);
process.exit(1);
}