const { status, data } = await axios.get(
`$ttps://localhost:3000/api/04/users/${username}`,
{
headers: {
authorization: process.env.API_TOKEN,
},
}
);
useEffect(() => {
async function fetchData() {
const req = await fetch(`/api/04/users?${username}`);
const data = await req.json();
setLoading(false);
setData(data);
}
fetchData();
}, []);
return (
<div>
<div>
<Link href="/" passHref>
Back to home
</Link>
</div>
<hr />
{loading && <div>Loading user data...</div>}
{data && <UserData user={data} />}
</div>
);
}
export default async function handler(req, res) {
if (!req.query.username) {
res.status(403).json({
error: 'Missing "username" query parameter',
});
return;
}
const username = req.query.username;
const API_ENDPOINT = process.env.API_ENDPOINT;
const API_TOKEN = process.env.API_TOKEN;
const userReq = await axios.get(`${API_ENDPOINT}/api/04/users/${username}`, {
headers: { authorization: API_TOKEN },
});
res.status(200).json(userReq.data);
}
→ 호출 코드 /api/singleUser?username=${username}
으로 변경
출처: 실전에서 바로 쓰는 Next.js