Spaces:
Running
Running
| ```javascript | |
| const express = require('express'); | |
| const path = require('path'); | |
| const app = express(); | |
| const PORT = 3000; | |
| // Middleware | |
| app.use(express.static(path.join(__dirname))); | |
| app.use(express.json()); | |
| app.use(express.urlencoded({ extended: true })); | |
| // Stockage en mémoire (à remplacer par une base de données) | |
| let users = []; | |
| let appointments = []; | |
| // Routes | |
| app.get('/', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'index.html')); | |
| }); | |
| app.get('/register', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'register.html')); | |
| }); | |
| app.get('/login', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'login.html')); | |
| }); | |
| app.get('/dashboard', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'dashboard.html')); | |
| }); | |
| // API endpoints | |
| app.post('/api/register', (req, res) => { | |
| const { name, email, password } = req.body; | |
| // Vérifier si l'utilisateur existe déjà | |
| const existingUser = users.find(u => u.email === email); | |
| if (existingUser) { | |
| return res.status(400).json({ error: 'Cet email est déjà utilisé' }); | |
| } | |
| // Créer un nouvel utilisateur | |
| const newUser = { name, email, password }; | |
| users.push(newUser); | |
| res.status(201).json({ message: 'Inscription réussie', user: newUser }); | |
| }); | |
| app.post('/api/login', (req, res) => { | |
| const { email, password } = req.body; | |
| // Vérifier les identifiants | |
| const user = users.find(u => u.email === email && u.password === password); | |
| if (!user) { | |
| return res.status(401).json({ error: 'Identifiants incorrects' }); | |
| } | |
| res.json({ message: 'Connexion réussie', user }); | |
| }); | |
| app.post('/api/appointments', (req, res) => { | |
| const { userId, laboratory, date, time, analysis } = req.body; | |
| // Créer un nouveau rendez-vous | |
| const newAppointment = { | |
| id: Date.now(), | |
| userId, | |
| laboratory, | |
| date, | |
| time, | |
| analysis, | |
| status: 'confirmé' | |
| }; | |
| appointments.push(newAppointment); | |
| res.status(201).json({ message: 'Rendez-vous pris avec succès', appointment: newAppointment }); | |
| }); | |
| app.get('/api/appointments/:userId', (req, res) => { | |
| const { userId } = req.params; | |
| const userAppointments = appointments.filter(a => a.userId === userId); | |
| res.json(userAppointments); | |
| }); | |
| app.delete('/api/appointments/:id', (req, res) => { | |
| const { id } = req.params; | |
| const appointmentId = parseInt(id); | |
| appointments = appointments.filter(a => a.id !== appointmentId); | |
| res.json({ message: 'Rendez-vous annulé' }); | |
| }); | |
| // Démarrer le serveur | |
| app.listen(PORT, () => { | |
| console.log(`Serveur démarré sur http://localhost:${PORT}`); | |
| }); | |
| ``` |