Cleanup index.ts

This commit is contained in:
Aelita4 2024-07-08 20:37:02 +02:00
parent 7be759bbc6
commit c53c517855
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 6 additions and 32 deletions

View File

@ -1,6 +1,5 @@
import express from 'express'; import express from 'express';
import bodyParser from 'body-parser'; import bodyParser from 'body-parser';
import Database from './db/database';
import fs from 'fs'; import fs from 'fs';
import authenticateJWT from './lib/authenticateJWT'; import authenticateJWT from './lib/authenticateJWT';
@ -9,46 +8,21 @@ const app = express();
app.use(bodyParser.urlencoded({extended : true})); app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json()); app.use(bodyParser.json());
// const db = new Database();
// type User = {
// username: string,
// password: string,
// role: string,
// xp: number
// };
const routes = 'routes'; const routes = 'routes';
app.all('*', (req, res, next) => {
console.log(`[${new Date().toLocaleString()}] ${req.method} ${req.url}`);
next();
});
fs.readdirSync(routes).forEach((file: string) => { fs.readdirSync(routes).forEach((file: string) => {
const route = require(`./${routes}/${file}`); const route = require(`./${routes}/${file}`);
app.use(`/${file.split('.')[0]}`, route.default); app.use(`/${file.split('.')[0]}`, route.default);
}); });
app.get('/', (req, res) => {
res.json({ message: 'Hello World!' });
});
app.get('/test', authenticateJWT, (req, res) => { app.get('/test', authenticateJWT, (req, res) => {
//@ts-ignore //@ts-ignore
res.json({ code: 200, message: `O chuj, zalogowałeś się ${req.user.username}!` }); res.json({ code: 200, message: `Authorization successful for user ${req.user.username}!` });
});
app.get('/db', (req, res) => {
const db = new Database();
db.query('INSERT INTO words VALUES (NULL, "kot", "猫", "animals", "neko")', []);
res.sendStatus(200);
}); });
app.listen(3000, () => { app.listen(3000, () => {