hassio-nextcloud-backup/nextcloud_backup/backend/src/app.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-09-27 23:38:40 +02:00
import cookieParser from "cookie-parser";
2024-02-18 17:19:37 +01:00
import express, { type NextFunction, type Request, type Response } from "express";
2022-09-27 23:38:40 +02:00
import createError from "http-errors";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";
2022-09-27 23:38:40 +02:00
import logger from "./config/winston.js";
import apiV2Router from "./routes/apiV2.js";
import cors from "cors"
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2019-12-19 15:08:47 +01:00
const app = express();
app.use(cors({
2023-01-13 16:18:27 +01:00
origin: true
}))
2022-09-27 23:38:40 +02:00
app.set("port", process.env.PORT || 3000);
// app.use(
// logger("dev", {
// skip: function (req, res) {
// return (res.statusCode = 304);
// },
// })
// );
2022-09-27 23:38:40 +02:00
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")));
2022-09-27 23:38:40 +02:00
app.use("/v2/api/", apiV2Router);
/*
-----------------------------------------------------------
Error handler
----------------------------------------------------------
*/
2019-12-19 15:08:47 +01:00
// catch 404 and forward to error handler
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
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
2022-09-27 23:38:40 +02:00
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
2020-02-15 14:28:48 +01:00
2022-09-27 23:38:40 +02:00
// render the error page
res.status(err.status || 500);
res.render("error");
2019-12-19 15:08:47 +01:00
});
export default app;