codex-mythica/app.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-10-15 20:37:26 +00:00
var express = require("express"),
path = require("path"),
logger = require("morgan"),
cookieParser = require("cookie-parser"),
compression = require("compression"),
routes = require("./routes/index"),
search = require("./routes/search"),
pkg = require("./package"),
app = express();
2018-01-08 00:12:11 +00:00
2024-10-15 20:37:26 +00:00
Object.assign(app.locals, {
mw_site_uri: "https://mythicwarsgame.com",
site: {
title: "The Codex Mythica",
description:
"The Codex Mythica is a database of all of the cards released for the Mythic Wars card game. Browse through all of the cards in the game, or search to find the card(s) you're looking for.",
keywords:
"codex, card game, mythic cards, mythic wars cards, mythic wars, clash of the gods, cthulhu rises, nemesis, excalibre, collectible card game, ccg, mythic sets, game, multiplayer, hobby, zeus, thor",
base_uri:
(process.env.NODE_ENV || "").toLowerCase().indexOf("dev") > -1
? "http://localhost:8320"
: "https://codex.mythicwarsgame.com",
},
author: {
name: pkg.author.name,
contact: pkg.author.email,
},
});
2018-01-08 00:12:11 +00:00
// view engine setup
2024-10-15 20:37:26 +00:00
app.set("views", path.join(__dirname, "views"));
2018-01-08 00:12:11 +00:00
// app.set('view engine', 'jade');
2024-10-15 20:37:26 +00:00
app.set("view engine", "ejs");
2018-01-08 00:12:11 +00:00
2024-10-15 20:37:26 +00:00
app.use(
logger(
':req[x-forwarded-for] - [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]'
)
);
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
2018-01-08 00:12:11 +00:00
app.use(cookieParser());
app.use((req, res, next) => {
2024-10-15 20:37:26 +00:00
if (req && req.originalUrl && res) {
res.locals.originalUrl = req.originalUrl;
}
next();
2018-01-08 00:12:11 +00:00
});
app.use(compression());
2024-10-15 20:37:26 +00:00
app.use("/", routes);
app.use("/search/", search);
2018-01-08 00:12:11 +00:00
2024-10-15 20:37:26 +00:00
app.use(express.static(path.join(__dirname, "public")));
2018-01-08 00:12:11 +00:00
/// catch 404 and forwarding to error handler
2024-10-15 20:37:26 +00:00
app.use(function (req, res, next) {
res.status(404).sendFile(
path.join(__dirname + "/public/errors", "404.html")
);
2018-01-08 00:12:11 +00:00
});
/// error handlers
// development error handler
// will print stacktrace
2024-10-15 20:37:26 +00:00
if ((process.env.NODE_ENV || "").toLowerCase().indexOf("dev") > -1) {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: err,
});
});
2018-01-08 00:12:11 +00:00
}
// production error handler
// no stacktraces leaked to user
2024-10-15 20:37:26 +00:00
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render("error", {
message: err.message,
error: {},
});
2018-01-08 00:12:11 +00:00
});
module.exports = app;