refont back express

This commit is contained in:
jleroy
2025-03-11 10:29:18 +01:00
parent f944b99172
commit 4e41294eee
18 changed files with 507 additions and 9 deletions

View File

@@ -0,0 +1,53 @@
import express from "express";
const router = express.Router();
import apiController from "../controllers/apiController.js";
import {
register,
login,
checkAuth,
logout,
} from "../controllers/authController.js";
import {verifyToken} from "../middleware/tokenJWTMiddleware.js";
import multer from "multer";
import { v4 as uuidv4 } from 'uuid';
import path from "node:path";
router.get('', apiController);
// Upload
const generateFileName = (originalName) => {
const ext = path.extname(originalName);
const now = new Date();
const timestamp = now.getFullYear().toString() +
String(now.getMonth() + 1).padStart(2, '0') +
String(now.getDate()).padStart(2, '0') +
String(now.getHours()).padStart(2, '0') +
String(now.getMinutes()).padStart(2, '0') +
String(now.getSeconds()).padStart(2, '0');
const uuid = uuidv4();
return `${timestamp}_${uuid}${ext}`;
};
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/'); // Stockage des fichiers dans le dossier uploads/
},
filename: function (req, file, cb) {
cb(null, generateFileName(file.originalname));
}
});
const upload = multer({ storage: storage });
router.post('/upload', verifyToken, upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).json({ message: 'Aucun fichier fourni' });
}
const filePath = `/uploads/${req.file.filename}`;
res.json({ filePath });
});
// Authentification et utilisateur
router.post('/auth/register', register);
router.post('/auth/login', login);
router.post('/auth/logout', logout);
router.get('/auth/check-auth', verifyToken, checkAuth);
export default router;