mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-23 04:10:40 +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'),
|
bodyParser = require('body-parser'),
|
||||||
port = process.env.PORT || 3000,
|
port = process.env.PORT || 3000,
|
||||||
mongoose = require("mongoose"),
|
mongoose = require("mongoose"),
|
||||||
|
@ -7,18 +8,22 @@ const session = require('express-session'),
|
||||||
app = express();
|
app = express();
|
||||||
|
|
||||||
require("dotenv").config();
|
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(session({ secret: 'secret', resave: true, saveUninitialized: true }));
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
app.use(express.static("public"));
|
app.use(express.static("public"));
|
||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
app.use(express.json());
|
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"))
|
for (const file of fs.readdirSync("./routes"))
|
||||||
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
|
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));
|
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();
|
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) => {
|
app.get("/", async (req, res) => {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
Akf-forum has got an API for other clients etc.
|
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
|
## Authorization
|
||||||
You need this headers for send request to API:
|
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?
|
## How to request?
|
||||||
|
|
||||||
### Request types:
|
### Request types:
|
||||||
- GET `/api/users/:id`
|
- GET `/api/users/:id` for fetch user.
|
||||||
- POST `/api/threads`
|
- POST `/api/users/:id/delete` for delete user.
|
||||||
- GET `/api/threads/:id`
|
- POST `/api/threads` for create thread.
|
||||||
- POST `/api/threads/:id/delete`
|
- GET `/api/threads/:id` for fetch thread.
|
||||||
- GET `/api/messages/:id`
|
- POST `/api/threads/:id/delete` for delete thread.
|
||||||
- POST `/api/messages`
|
- GET `/api/messages/:id` for fetch message.
|
||||||
- POST `/api/messages/:id/delete`
|
- POST `/api/messages` for create message.
|
||||||
- POST `/api/messages/:id/react/:type`
|
- POST `/api/messages/:id/delete` for delete message.
|
||||||
|
- POST `/api/messages/:id/react/:type` for react to a message.
|
||||||
|
|
||||||
### Example request:
|
### Example request:
|
||||||
```GET /api/message/1```
|
```GET /api/message/1```
|
||||||
|
|
Loading…
Reference in a new issue