Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
404 error when trying to reach a defined route

I'm getting a 404 error for a search route despite similarly defined routes working fine. I'm trying to search for a post via its title and/or tags. Here are my routes defined in the backend:

const router = express.Router();

router.get("/", getPosts);
router.get("/:id", getPost);
router.get("/search", getPostsBySearch);
router.post("/", auth, createPost);
router.patch("/:id", auth, updatePost);
router.patch("/:id/likePost", auth, likePost);
router.delete("/:id", auth, deletePost);

export default router

Here is my action for searching for a post:

export const getPostsBySearch = (searchQuery) => async (dispatch) => {
  try {
    dispatch({ type: "START_LOADING" });
    const { data } = await api.fetchPostsBySearch(searchQuery);
    dispatch({ type: "FETCH_BY_SEARCH", payload: data });
    dispatch({ type: "END_LOADING" });
  } catch (error) {
    console.log(error);
  }
}

And here is the api file exporting routes to the action:

const API = axios.create({ baseURL: "http://localhost:5000" });

export const fetchPosts = (page) => API.get(`/posts?page=${page}`);
export const fetchPost = (id) => API.get(`/posts/${id}`);
export const fetchPostsBySearch = (searchQuery) =>
  API.get(
    `/posts/search?searchQuery=${searchQuery.search || "none"}&tags=${
      searchQuery.tags
    }`
  );
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I had the :id route on top of the search route, so it was reading /search as an id, so instead of

router.get("/", getPosts);
router.get("/:id", getPost);
router.get("/search", getPostsBySearch);

it should be

router.get("/", getPosts);
router.get("/search", getPostsBySearch);
router.get("/:id", getPost);
about 4 years ago · Juan Pablo Isaza Report

0

router.get("/search", getPostsBySearch); // Route you mentioned
but you were using `/posts/search`

export const fetchPostsBySearch = (searchQuery) =>
  API.get(
    `/search?searchQuery=${searchQuery.search || "none"}&tags=${
      searchQuery.tags
    }`
  );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!