-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Ajout d'un fichier api/random - Génération d'un nombre aléatoire de user dans la réponse
- Loading branch information
Showing
2 changed files
with
38 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.