mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-12-22 23:59:08 +03:00
not required, middlewares folder
This commit is contained in:
parent
97a9444cc7
commit
9fa9e6f9be
11 changed files with 23 additions and 25 deletions
13
index.js
13
index.js
|
@ -1,4 +1,5 @@
|
|||
const session = require('express-session'),
|
||||
const { UserModel } = require("./models"),
|
||||
session = require('express-session'),
|
||||
bodyParser = require('body-parser'),
|
||||
port = process.env.PORT || 3000,
|
||||
mongoose = require("mongoose"),
|
||||
|
@ -7,18 +8,22 @@ const session = require('express-session'),
|
|||
app = express();
|
||||
|
||||
require("dotenv").config();
|
||||
mongoose.connect(process.env.MONGO_DB_URL || "mongodb://localhost:27017/akf-forum", () => console.log("Database is connected"));
|
||||
mongoose.connect(process.env.MONGO_DB_URL, () => console.log("Database is connected"));
|
||||
|
||||
app.use(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(express.static("public"));
|
||||
app.set("view engine", "ejs");
|
||||
app.use(express.json());
|
||||
app.use(require("./middlewares/user"));
|
||||
app.use(async (req, res, next) => {
|
||||
res.error = (type, error) => res.status(type).render("error", { type, error });
|
||||
req.user = await UserModel.get(req.session.userid);
|
||||
next();
|
||||
});
|
||||
|
||||
for (const file of fs.readdirSync("./routes"))
|
||||
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
|
||||
|
||||
app.all("*", (req, res) => res.status(404).render("error", { type: 404, error: "We have not got this page." }));
|
||||
app.all("*", (req, res) => res.error(404, "We have not got this page."));
|
||||
|
||||
app.listen(port, () => console.log("akf-forum on port:", port));
|
|
@ -1,4 +0,0 @@
|
|||
module.exports = (req, res, next) => {
|
||||
if (!req.session.userid) return res.redirect('/login');
|
||||
next();
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
const { UserModel } = require("../models");
|
||||
|
||||
module.exports = async (req, res, next) => {
|
||||
res.error = (type, error) => res.status(type).render("error", { type, error });
|
||||
req.user = await UserModel.get(req.session.userid);
|
||||
next();
|
||||
}
|
|
@ -4,7 +4,10 @@ const { Router } = require("express")
|
|||
|
||||
const app = Router();
|
||||
|
||||
app.use(require("../middlewares/login"));
|
||||
app.use((rq,rs,n)=>{
|
||||
if (!rq.session.userid) return rs.redirect('/login');
|
||||
n();
|
||||
});
|
||||
|
||||
app.get("/", async (req, res) => {
|
||||
const user = req.user;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
Akf-forum has got an API for other clients etc.
|
||||
|
||||
You can find examples in `/tests` folder.
|
||||
You can find examples in `tests` folder.
|
||||
|
||||
## Authorization
|
||||
You need this headers for send request to API:
|
||||
|
@ -17,14 +17,15 @@ You need this headers for send request to API:
|
|||
## How to request?
|
||||
|
||||
### Request types:
|
||||
- GET `/api/users/:id`
|
||||
- POST `/api/threads`
|
||||
- GET `/api/threads/:id`
|
||||
- POST `/api/threads/:id/delete`
|
||||
- GET `/api/messages/:id`
|
||||
- POST `/api/messages`
|
||||
- POST `/api/messages/:id/delete`
|
||||
- POST `/api/messages/:id/react/:type`
|
||||
- GET `/api/users/:id` for fetch user.
|
||||
- POST `/api/users/:id/delete` for delete user.
|
||||
- POST `/api/threads` for create thread.
|
||||
- GET `/api/threads/:id` for fetch thread.
|
||||
- POST `/api/threads/:id/delete` for delete thread.
|
||||
- GET `/api/messages/:id` for fetch message.
|
||||
- POST `/api/messages` for create message.
|
||||
- POST `/api/messages/:id/delete` for delete message.
|
||||
- POST `/api/messages/:id/react/:type` for react to a message.
|
||||
|
||||
### Example request:
|
||||
```GET /api/message/1```
|
||||
|
|
Loading…
Reference in a new issue