-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (78 loc) · 2.58 KB
/
server.js
File metadata and controls
107 lines (78 loc) · 2.58 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import * as dotenv from "dotenv"
dotenv.config()
import express from "express"
const app = express()
import morgan from "morgan"
import mongoose from "mongoose"
import { authenticateUser } from "./middleware/authMiddleware.js"
import cookieParser from "cookie-parser"
// import { validateTest } from "./middleware/validationMiddleware.js"
import {v2 as cloudinary} from 'cloudinary';
// routers
import productRouter from "./routes/productRouter.js"
import authRouter from "./routes/authRouter.js"
import userRouter from "./routes/userRouter.js"
// public
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import path from 'path';
// middleware
import errorHandleMiddleware from "./middleware/errorHandlerMiddleware.js"
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET
});
// represent the directory name of the current module
const __dirname = dirname(fileURLToPath(import.meta.url));
if(process.env.NODE_ENV === "development") {
app.use(morgan("dev"))
}
// middleware to server static assets (images, stylesheets, ect) to the frontend
app.use(express.static(path.resolve(__dirname, './client/dist')));
// parse cookies so you can use in your app
app.use(cookieParser())
// automatically parse incoming JSON requests
app.use(express.json())
app.get("/", (req, res) => {
res.send("hello world!")
})
// app.post("/api/v1/test",
// validateTest,
// (req, res) => {
// const { name } = req.body
// console.log(req)
// res.json({ message: `hello ${name}` })
// })
app.get("/api/v1/test", (req, res) => {
res.json({ msg:"test route" })
})
// base route for products
app.use("/api/v1/products", authenticateUser, productRouter)
// base route for auth
app.use("/api/v1/users", authenticateUser, userRouter)
app.use("/api/v1/auth", authRouter)
/*
This is a fallback route that lets
the client side take control of the routing
*/
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './client/dist', 'index.html'));
});
// used when a request to a route doesn't exist
app.use("*", (req, res) => {
res.status(404).json({msg: "not found"})
})
// used to handle any error that happens during the process of the request
app.use(errorHandleMiddleware)
const port = process.env.PORT || 5100
// connect to the MongoDb database and start the express server
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)
}