-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (57 loc) · 1.88 KB
/
index.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
import express from "express";
import session from "express-session";
import SequelizeStore from "connect-session-sequelize";
import FileUpload from "express-fileupload"; //depedencies upload data
import cors from "cors"; //ini digunakan untuk agar API dapat di askes dari luar domain
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import db from "./config/database.js"; //aktifkan saat syn table
//config
dotenv.config()
//table data
import loginRoute from "./routes/AuthRoute.js";
import userRoute from "./routes/UserRoute.js";
import productRoute from "./routes/ProductRoute.js";
const app=express();
const sessionStore = SequelizeStore(session.Store);
const store = new sessionStore({
db: db,
});
//session
app.use(session({
secret:process.env.SESS_SECRET,
resave: false,
saveUninitialized : true,
store: store,
cookie : {
secure: 'auto', //pilih true kalo https
}
}));
//as middelware
const corsOptions ={
origin:'http://localhost:3000', //domain yang kita izinkan untuk akses api
credentials:true, //access-control-allow-credentials:true
optionSuccessStatus:200
}
app.use(cors(corsOptions)); //cred : agar klien mengirim credendsial //origin : domain untuk dapat mengskses API
app.use(cookieParser()); //ini digunakan utk membuat refresh token agar setiap token expire tidak perlu login lagi
//express json
app.use(express.json()); // menerima data dalam format json
//file upload fungsi
app.use(FileUpload());
//static file
app.use(express.static("public"));
//fungsi routes
app.use(loginRoute);
app.use(userRoute);
app.use(productRoute);
//membuat table session
// store.sync()
//sync untuk generate table
// aktifkan ini jika tidak ada table
// (async () => {
// await db.sync();
// })();
//server run
app.listen(process.env.APP_PORT, ()=> console.log(`server berjalan....`));
//install nodemon secar global untuk