Skip to content
Snippets Groups Projects
Commit ac5636ad authored by epson220's avatar epson220
Browse files

searchBoardByKeyword

parent c4b82889
No related branches found
No related tags found
No related merge requests found
...@@ -487,4 +487,20 @@ router.post("/profile", async function (req, res) { ...@@ -487,4 +487,20 @@ router.post("/profile", async function (req, res) {
} }
}); });
router.post("/searchBoard", async function (req, res) {
console.log("/searchBoard post 요청");
console.dir(req.body);
try {
let searched = await BoardModel.find({
title: { $regex: req.body.input },
});
console.log(searched);
res.send(searched);
} catch (err) {
console.log(err);
}
});
module.exports = router; module.exports = router;
This diff is collapsed.
...@@ -4,6 +4,11 @@ import axios from "axios"; ...@@ -4,6 +4,11 @@ import axios from "axios";
const Board = () => { const Board = () => {
const [response, setResponse] = useState([]); const [response, setResponse] = useState([]);
const [input, setInput] = useState("");
const handleInputChange = (e) => {
setInput(e.target.value);
console.log(input);
};
useEffect(async () => { useEffect(async () => {
// try { // try {
...@@ -19,13 +24,27 @@ const Board = () => { ...@@ -19,13 +24,27 @@ const Board = () => {
// } // }
async function fetchData() { async function fetchData() {
console.log("react board get axios 호출"); console.log("react board get axios 호출");
const result = await axios.get("http://localhost:3001/board"); const result = await axios.get("http://localhost:3001/board", {
input: input,
});
console.log(result.data); console.log(result.data);
setResponse(result.data); setResponse(result.data);
} }
fetchData(); fetchData();
}, []); }, []);
const handleOnSubmit = () => {
async function fetchData2() {
console.log("react board search axios 호출");
const result2 = await axios.post("http://localhost:3001/searchBoard", {
input: input,
});
console.log(result2.data);
setResponse(result2.data);
}
fetchData2();
};
return ( return (
<div> <div>
<button> <button>
...@@ -34,10 +53,16 @@ const Board = () => { ...@@ -34,10 +53,16 @@ const Board = () => {
<button> <button>
<Link to="/updateProf">프로필 작성</Link> <Link to="/updateProf">프로필 작성</Link>
</button> </button>
<form action="http://localhost:3001/searchBoard" method="post">
<input type="text" name="searchKeyword"></input> <input
<button type="submit">게시글검색</button> type="text"
</form> //name="searchKeyword"
onChange={handleInputChange}
></input>
<button type="submit" onClick={handleOnSubmit}>
게시글검색
</button>
<ol> <ol>
{response.map((res) => ( {response.map((res) => (
<li key={res._id}> <li key={res._id}>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment