-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (56 loc) · 1.63 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
// Import dotenv
import dotenv from "dotenv";
dotenv.config();
// Import dependencies
import createError from "http-errors";
import express from "express";
import cookieParser from "cookie-parser";
import logger from "morgan";
// Import routers and config
import usersRouter from "./routes/users.js";
import attendanceRouter from "./routes/attendance.js";
import payrollRouter from "./routes/payroll.js";
import authRouter from "./routes/auth.js";
import sequelize from "./config/index.js";
const app = express();
// Middleware
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// Routes
app.use("/users", usersRouter);
app.use("/attendance", attendanceRouter);
app.use("/payroll", payrollRouter);
app.use("/auth", authRouter);
// Sync the database
sequelize
.sync()
.then(() => {
console.log("Database synchronized");
})
.catch((err) => {
console.error("Error synchronizing database:", err);
});
// Set up server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${3000}`);
});
app.get("/", (req, res) => {
res.send("Selamat datang di API Absensi dan Payroll Karyawan");
});
// Catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// Error handler
app.use((err, req, res, next) => {
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// Send error response
res.status(err.status || 500).send({
message: err.message,
error: req.app.get("env") === "development" ? err : {},
});
});