2022-08-11 03:12:40 +03:00
|
|
|
const { UserModel } = require("./models"),
|
|
|
|
session = require('express-session'),
|
2022-03-22 00:16:47 +03:00
|
|
|
bodyParser = require('body-parser'),
|
2022-08-09 19:16:34 +03:00
|
|
|
port = process.env.PORT || 3000,
|
2022-04-06 21:14:46 +03:00
|
|
|
mongoose = require("mongoose"),
|
2022-03-22 00:16:47 +03:00
|
|
|
express = require('express'),
|
|
|
|
fs = require("fs"),
|
|
|
|
app = express();
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-08-09 19:16:34 +03:00
|
|
|
require("dotenv").config();
|
2022-08-11 03:12:40 +03:00
|
|
|
mongoose.connect(process.env.MONGO_DB_URL, () => console.log("Database is connected"));
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-13 16:16:46 +03:00
|
|
|
app.use(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2022-03-22 00:10:29 +03:00
|
|
|
app.use(express.static("public"));
|
2022-03-13 16:16:46 +03:00
|
|
|
app.set("view engine", "ejs");
|
2022-03-22 00:10:29 +03:00
|
|
|
app.use(express.json());
|
2022-08-11 03:12:40 +03:00
|
|
|
app.use(async (req, res, next) => {
|
|
|
|
req.user = await UserModel.get(req.session.userid);
|
2022-08-26 19:39:25 +03:00
|
|
|
res.error = (type, error) => res.status(type).render("error", {user: req.user, type, error });
|
2022-08-24 22:10:23 +03:00
|
|
|
if (req.user?.deleted) {
|
2022-08-24 22:09:21 +03:00
|
|
|
req.session.destroy();
|
|
|
|
return res.error(403, "Your account has been deleted.");
|
|
|
|
}
|
2022-08-11 03:12:40 +03:00
|
|
|
next();
|
|
|
|
});
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-03-22 00:10:29 +03:00
|
|
|
for (const file of fs.readdirSync("./routes"))
|
2022-04-06 21:14:46 +03:00
|
|
|
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-11 03:12:40 +03:00
|
|
|
app.all("*", (req, res) => res.error(404, "We have not got this page."));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-11 00:38:44 +03:00
|
|
|
app.listen(port, () => console.log("akf-forum on port:", port));
|