Skip to content

Commit

Permalink
✨ Requête API random add
Browse files Browse the repository at this point in the history
- Ajout d'un fichier api/random
- Génération d'un nombre aléatoire de user dans la réponse
  • Loading branch information
chambrin committed Sep 5, 2023
1 parent c218b3b commit c67c6b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
38 changes: 38 additions & 0 deletions app/api/random/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


export async function GET(request: Request, res: Response) {
let randomId = Math.floor(Math.random() * 40) + 1; // generate a random number between 1 and 40
let url = `http://localhost:3001/api/user/${randomId}`; // replace with your API's URL

// Fetch the user's data
try {
let userResponse = await fetch(url);
if (!userResponse.ok) {
throw new Error(`HTTP error! status: ${userResponse.status}`);
}
let data = await userResponse.json();

// Return the user's data
return new Response(JSON.stringify(data), {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
} catch(e) {
console.log('Error fetching user:', e);

return new Response(JSON.stringify({error: 'An error occurred'}), {
status: 500,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
})
}
}
8 changes: 0 additions & 8 deletions app/aut/page.tsx

This file was deleted.

0 comments on commit c67c6b4

Please sign in to comment.