-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (68 loc) · 2.04 KB
/
app.js
File metadata and controls
84 lines (68 loc) · 2.04 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
import express from "express"
import session from 'express-session'
import mongoose from "mongoose"
import MongoStore from "connect-mongo"
import route from "./router/userroute.js"
import router from "./router/adminroute.js"
import cors from "cors"
import dotenv from 'dotenv'
dotenv.config()
console.log(process.env.PORT);
const app = express()
app.use(cors({
origin:process.env.CORS_URI,
credentials:true
}));
// const allowedOrigins =process.env.CORS_URI.split(",")
// app.use(cors({
// origin:(origin,callback)=>{
// if(!origin || allowedOrigins.includes(origin)){
// callback(null,true)
// }
// else{
// callback(new Error("not allowed by cors"))
// }
// },
// credentials: true,
// methods: ['GET','POST','PUT','PATCH','DELETE','OPTIONS']
// }));
app.use(express.json())
app.use(express.urlencoded({extended:true}))
await mongoose.connect(process.env.MONGO_URI)
app.use(session({
secret:process.env.SESSION_SECRET,
resave:false,
saveUninitialized:false,
store:MongoStore.create({mongoUrl:process.env.MONGO_URI}),
cookie: {
httpOnly: true, // prevents JS access
secure: false, // true only if you use HTTPS
sameSite: "lax", // allow cookies to work cross-origin in dev
maxAge: 1000 * 60 * 60 * 24 // 1 day
}
}))
app.use('/api',route)
app.use('/api',router)
app.use('/uploads',express.static('uploads'))
app.use((req,res,next)=>{
res.locals.message=req.session.message
delete req.session.message
next()
})
//page not found
// app.all("*", (req, res, next) => {
// const error = new Error(`Route ${req.originalUrl} not found`);
// error.status = 404;
// next(error);
// });
// // Global error handler (must be the last middleware)
// app.use((err, req, res, next) => {
// console.error("Error:", err.message);
// res.status(err.status || 500).json({
// success: false,
// message: err.message || "Internal Server Error",
// });
// });
app.listen(process.env.PORT,()=>{
console.log(`server listening at port:${process.env.PORT}`);
})