Better index.js

This commit is contained in:
Akif9748 2022-09-16 22:40:43 +03:00
parent ba0d4cbb8d
commit a816aab7eb
5 changed files with 22 additions and 21 deletions

View file

@ -42,7 +42,7 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
- upload other photos, model for it
- category system bloat.
- replace not found errors with no perm
- prewiev for send messages in markdown format.
- prewiev for send messages in markdown format. Markdown in user about
## Major Version History
- V4: Caching
- V3: New Theme

View file

@ -1,5 +1,5 @@
{
"def_theme": "default",
"forum_name": "akf",
"desp": "Akf-forum!"
"description": "Akf-forum!"
}

View file

@ -1,10 +1,12 @@
const { def_theme, forum_name, desp } = require("./config.json"),
const { urlencoded: BP } = require('body-parser'),
{ mw: IP } = require('request-ip'),
RL = require('express-rate-limit'),
BAN = require('express-ip-block'),
SES = require('express-session');
const
{ def_theme, forum_name, description } = require("./config.json"),
{ UserModel, BanModel } = require("./models"),
rateLimit = require('express-rate-limit'),
ipBlock = require('express-ip-block'),
session = require('express-session'),
bodyParser = require('body-parser'),
requestIp = require('request-ip'),
port = process.env.PORT || 3000,
mongoose = require("mongoose"),
express = require('express'),
@ -19,16 +21,15 @@ mongoose.connect(process.env.MONGO_DB_URL,
app.set("view engine", "ejs");
app.use(
session({ secret: 'secret', resave: true, saveUninitialized: true }),
express.static("public"), express.json(), ipBlock(app.ips), requestIp.mw(),
app.use(express.static("public"), express.json(), IP(), BAN(app.ips),
SES({ secret: 'secret', resave: true, saveUninitialized: true }),
async (req, res, next) => {
req.user = req.session.userID ? await UserModel.findOneAndUpdate({ id: req.session.userID }, {
lastSeen: Date.now(), $addToSet: { ips: req.clientIp }
}) : null;
res.reply = (page, options = {}, status = 200) => res.status(status)
.render(page, { user: req.user, theme: req.user?.theme || def_theme, forum_name, desp, ...options });
.render(page, { user: req.user, theme: req.user?.theme || def_theme, forum_name, description, ...options });
res.error = (type, error) => res.reply("error", { type, error }, type);
@ -37,10 +38,10 @@ app.use(
return res.error(403, "Your account has been deleted.");
}
next();
}, rateLimit({
}, RL({
windowMs: 60_000, max: 20,
handler: (req, res, next, opts) => !req.user?.admin ? res.error(opts.statusCode, "You are begin ratelimited") : next()
}), bodyParser.urlencoded({ extended: true })
}), BP({ extended: true })
);
for (const file of fs.readdirSync("./routes"))

View file

@ -1,8 +1,8 @@
const { Router, request, response } = require("express")
const { Router, request, response } = require("express");
const app = Router();
const fs =require("fs")
const fs = require("fs");
const bcrypt = require("bcrypt");
const { SecretModel, UserModel } = require("../../models")
const { SecretModel, UserModel } = require("../../models");
/**
* Auth checker
@ -35,9 +35,9 @@ app.use(async (req, res, next) => {
app.get("/me", (req, res) => res.complate(req.user))
for (const file of fs.readdirSync("./routes/api/routes"))
for (const file of fs.readdirSync("./routes/api/routes"))
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
app.all("*", (req, res) => res.error(400, "Bad request"));
module.exports = app;

View file

@ -2,7 +2,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><%= title || forum_name +"-forum" %></title>
<meta name="description" content="<%= desp %>">
<meta name="description" content="<%= description %>">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<link rel="stylesheet" href="/css/themes/<%= theme %>.css" />
<link rel="stylesheet" href="/css/common.css" />