MusicTopus/src/app.js

131 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-02-13 17:59:42 +01:00
import path from "path";
import express from "express";
import cookieParser from "cookie-parser";
import passport from "passport";
import mongoose from "mongoose";
import flash from "connect-flash";
import session from "express-session";
import MongoStore from "connect-mongo";
2022-02-13 14:03:04 +01:00
2022-02-13 17:59:42 +01:00
import config, { env, mongoDbUri, secret } from "./config";
2022-02-15 16:45:14 +01:00
import { isXhr } from "./helpers";
2022-02-15 11:03:20 +01:00
import indexRouter from "./routes";
2022-02-15 11:03:20 +01:00
import importAlbumRouterApiV1 from "./routes/api/v1/albums";
import importSearchRouterApiV1 from "./routes/api/v1/search";
2022-02-13 17:59:42 +01:00
// Mongoose schema init
require("./models/users");
2022-02-15 11:03:20 +01:00
require("./models/albums");
2022-02-13 17:59:42 +01:00
require("./libs/passport")(passport);
mongoose
.connect(mongoDbUri, { useNewUrlParser: true, useUnifiedTopology: true })
.catch(() => {
process.exit();
});
const sess = {
cookie: {
maxAge: 86400000,
},
secret,
saveUninitialized: false,
resave: false,
store: MongoStore.create({
mongoUrl: mongoDbUri,
mongoOptions: { useNewUrlParser: true, useUnifiedTopology: true },
}),
};
2022-02-13 14:03:04 +01:00
const app = express();
app.use(express.json());
2022-02-13 17:59:42 +01:00
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(flash());
app.use(session(sess));
if (["production"].indexOf(env) !== -1) {
app.enable("trust proxy", 1);
sess.cookie.secure = true;
/* eslint-disable func-names */
app.use((req, res, next) => {
if (req.secure) {
next();
} else {
res.redirect(`https://${req.headers.host}${req.url}`);
}
});
}
app.use(passport.initialize());
app.use(passport.session());
2022-02-15 11:03:20 +01:00
app.set("views", path.join(__dirname, "../views"));
2022-02-13 17:59:42 +01:00
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "../public")));
2022-02-15 11:03:20 +01:00
app.use(
"/libs/vue",
express.static(path.join(__dirname, "../node_modules/vue/dist"))
);
app.use(
"/libs/axios",
express.static(path.join(__dirname, "../node_modules/axios/dist"))
2022-02-13 17:59:42 +01:00
);
app.use("/", indexRouter);
2022-02-15 11:03:20 +01:00
app.use("/api/v1/albums", importAlbumRouterApiV1);
app.use("/api/v1/search", importSearchRouterApiV1);
2022-02-13 17:59:42 +01:00
// Handle 404
app.use((req, res) => {
2022-02-15 16:45:14 +01:00
if (isXhr(req)) {
2022-02-13 17:59:42 +01:00
res.status(404).send({ message: "404: Not found" });
} else {
2022-02-15 11:03:20 +01:00
res.status(404).render("index", {
2022-02-13 17:59:42 +01:00
page: { title: `404: Cette page n'existe pas.` },
errorCode: 404,
2022-02-15 11:03:20 +01:00
viewname: "error",
2022-02-13 17:59:42 +01:00
user: req.user || null,
config,
session: req.session || null,
flashInfo: null,
query: null,
params: null,
2022-02-15 11:03:20 +01:00
error: null,
2022-02-13 17:59:42 +01:00
});
}
});
// Handle 500
app.use((error, req, res, next) => {
2022-02-15 16:45:14 +01:00
if (isXhr(req)) {
2022-02-15 11:03:20 +01:00
const { message, errorCode, date } = error;
res.status(error.errorCode || 500).send({ message, errorCode, date });
2022-02-13 17:59:42 +01:00
} else {
2022-02-15 11:03:20 +01:00
res.status(error.errorCode || 500);
res.render("index", {
2022-02-13 17:59:42 +01:00
page: { title: "500: Oups… le serveur a crashé !", error },
errorCode: error.errorCode || 500,
2022-02-15 11:03:20 +01:00
viewname: "error",
2022-02-13 17:59:42 +01:00
user: req.user || null,
config,
session: req.session || null,
flashInfo: null,
query: null,
params: null,
2022-02-15 11:03:20 +01:00
error: null,
2022-02-13 17:59:42 +01:00
});
2022-02-13 14:03:04 +01:00
2022-02-13 17:59:42 +01:00
next();
}
});
2022-02-13 14:03:04 +01:00
export default app;