try{constresponse=awaitfetch("http://localhost:3000/api/sample");if(!response.ok){// HTTPステータスコードが200-299の範囲ではないthrownewResponse("Internal server error",response);}constbody=awaitresponse.json();}catch(error){thrownewResponse("Internal server error",{status:500});}
POST の例
1 2 3 4 5 6 7 8 91011121314
try{constresponse=awaitfetch("http://localhost:3000/api/sample",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:"value"}),});if(!response.ok){// HTTPステータスコードが200-299の範囲ではないthrownewResponse("Internal server error",response);}constbody=awaitresponse.json();}catch(error){thrownewResponse("Internal server error",{status:500});}