-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-middleware.js
40 lines (37 loc) · 1.05 KB
/
custom-middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React, { useState } from 'react';
const Page = () => {
const [userMessage] = useState("What's Popin?");
const [response, setResponse] = useState(null);
const [isPosting, setIsPosting] = useState(false);
const handleClick = async () => {
setIsPosting(true);
try {
const response = await fetch('/api/custom-middleware', {
method: 'POST',
body: JSON.stringify({ message: userMessage })
});
const data = await response.json();
setTimeout(() => {
setResponse(data);
setIsPosting(false);
}, 1000);
} catch (error) {
setIsPosting(false);
setResponse(error);
}
};
return (
<main>
<h1>Restrict access using CORS middleware</h1>
{response ? (
<pre>{JSON.stringify(response, null, 2)}</pre>
) : (
<pre>{JSON.stringify({ message: userMessage }, null, 2)}</pre>
)}
<button onClick={handleClick} disabled={isPosting}>
{isPosting ? 'Sending...' : 'Send message'}
</button>
</main>
);
};
export default Page;