2022-09-27 23:38:40 +02:00
|
|
|
import cookieParser from "cookie-parser";
|
2024-07-11 15:47:27 +02:00
|
|
|
import cors from "cors";
|
|
|
|
import errorHandler from "errorhandler";
|
|
|
|
import express from "express";
|
2022-09-27 23:38:40 +02:00
|
|
|
import createError from "http-errors";
|
|
|
|
import morgan from "morgan";
|
2022-04-30 16:19:31 +02:00
|
|
|
import path from "path";
|
2022-09-21 17:24:02 +02:00
|
|
|
import { fileURLToPath } from "url";
|
2022-09-27 23:38:40 +02:00
|
|
|
import logger from "./config/winston.js";
|
|
|
|
import apiV2Router from "./routes/apiV2.js";
|
2022-04-30 16:19:31 +02:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
2022-09-21 17:24:02 +02:00
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2019-12-19 15:08:47 +01:00
|
|
|
const app = express();
|
2022-09-21 17:24:02 +02:00
|
|
|
|
2024-07-11 15:47:27 +02:00
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: true,
|
|
|
|
})
|
|
|
|
);
|
2022-10-13 17:11:55 +02:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
app.set("port", process.env.PORT || 3000);
|
|
|
|
|
|
|
|
app.use(
|
2022-11-14 16:24:55 +01:00
|
|
|
morgan("dev", { stream: { write: (message) => logger.debug(message) } })
|
2022-09-27 23:38:40 +02:00
|
|
|
);
|
2019-12-19 15:08:47 +01:00
|
|
|
app.use(express.json());
|
2020-11-09 12:42:26 +01:00
|
|
|
app.use(express.urlencoded({ extended: false }));
|
2019-12-19 15:08:47 +01:00
|
|
|
app.use(cookieParser());
|
2020-11-09 12:42:26 +01:00
|
|
|
app.use(express.static(path.join(__dirname, "public")));
|
2021-01-08 01:49:21 +01:00
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
app.use("/v2/api/", apiV2Router);
|
2021-01-08 13:41:36 +01:00
|
|
|
/*
|
|
|
|
-----------------------------------------------------------
|
|
|
|
Error handler
|
|
|
|
----------------------------------------------------------
|
|
|
|
*/
|
2019-12-19 15:08:47 +01:00
|
|
|
// catch 404 and forward to error handler
|
2022-09-21 17:24:02 +02:00
|
|
|
app.use((req, res, next) => {
|
2022-09-27 23:38:40 +02:00
|
|
|
next(createError(404));
|
2019-12-19 15:08:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
2024-07-11 15:47:27 +02:00
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
// only use in development
|
|
|
|
app.use(errorHandler());
|
|
|
|
}
|
2021-01-08 13:41:36 +01:00
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
export default app;
|