From f1b0fa32ad49094acfc76632d19deef5d5d60e20 Mon Sep 17 00:00:00 2001 From: anys34 Date: Sun, 27 Aug 2023 15:29:42 +0900 Subject: [PATCH] FIX :: search users --- app.js | 2 - routes/search.js | 99 ++++++++++++++++++++++++------------------------ 2 files changed, 50 insertions(+), 51 deletions(-) diff --git a/app.js b/app.js index 1f4f026..fe55cfd 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,6 @@ const session = require("express-session"); const pageRouter = require("./routes/page"); const authRouter = require("./routes/auth"); -const userRouter = require("./routes/user"); const searchRouter = require("./routes/search"); const app = express(); @@ -30,7 +29,6 @@ app.use( app.use("/", pageRouter); app.use("/auth", authRouter); -app.use("/user", userRouter); app.use("/search", searchRouter); app.listen(app.get("port"), () => { diff --git a/routes/search.js b/routes/search.js index 2443861..ea075a6 100644 --- a/routes/search.js +++ b/routes/search.js @@ -7,7 +7,7 @@ require("dotenv").config(); const env = process.env; const header = `headers: { - Authorization: "Bearer " + ${env.GITHUB_TOKEN}, + Authorization: Bearer ${env.GITHUB_TOKEN}, }`; router.get("/:id", async (req, res) => { @@ -34,67 +34,68 @@ router.get("/:id", async (req, res) => { }); res.send(user_Info); } catch (error) { - console.log("API 호출 실패:", error.response.status); - console.log(error.response.data); + console.log(error); + res.status(500).send(error); } }); router.get("/users/:id", async (req, res) => { const { id } = req.params; const page = req.query.page; - let users_Info = []; - axios - .get( + try { + const response = await axios.get( `https://api.github.com/search/users?q=${id} type:user&per_page=10&page=${page}`, { header, } - ) - .then(async (response) => { - const users = response.data; - const requests = users.items.map((item) => { - if (item === undefined) { - return null; - } - return axios.get(item.url, { - header, - }); + ); + const users = response.data; + const requests = users.items.map((item) => { + if (item === undefined) { + return null; + } + return axios.get(item.url, { + header, }); + }); - const responses = await Promise.all(requests); + const responses = await Promise.all(requests); - const users_Info = await Promise.all( - responses.map(async (user_Info) => { - if (user_Info) { - const user = user_Info.data; - const commit = await getCommitCount(user.html_url); - return { - profile_img: user.avatar_url, - id: user.login, - url: user.html_url, - name: user.name, - bio: user.bio, - company: user.company, - public_repos: user.public_repos, - followers: user.followers, - following: user.following, - location: user.location, - commit: commit, - }; - } - }) - ); - if (users_Info.length > 0 || page == 1) { - users_Info.push({ - total: users.total_count, - }); - } - res.send(users_Info); - }) - .catch((error) => { - console.log("API 호출 실패:", error.response.status); - console.log(error.response.data); - }); + const users_Info = await Promise.all( + responses.map(async (user_Info) => { + if (user_Info) { + const user = user_Info.data; + const commit = await getCommitCount(user.html_url); + return { + profile_img: user.avatar_url, + id: user.login, + url: user.html_url, + name: user.name, + bio: user.bio, + company: user.company, + public_repos: user.public_repos, + followers: user.followers, + following: user.following, + location: user.location, + commit: commit, + }; + } + }) + ); + if (users_Info.length > 0 || page == 1) { + users_Info.push({ + total: users.total_count, + }); + return res.send(users_Info); + } + if (!(users_Info.length > 0)) { + return res.send(null); + } + res.send(users_Info); + } catch (error) { + console.log(error); + res.status(500).send(error); + } }); async function getCommitCount(url) {