refont back express
This commit is contained in:
53
backend/routes/apiRoutes.js
Normal file
53
backend/routes/apiRoutes.js
Normal 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;
|
||||
Reference in New Issue
Block a user