Next.js Discord

Discord Forum

Internal API Call in the same NextJS App not working - 404 not found

Answered
Yacare Caiman posted this in #help-forum
Open in Discord
Yacare CaimanOP
Hello I followed this tutorial, to create an internal API EndPoint as server and make the API call in React with 'use client'. But it didn't worked for me: https://nextjs.org/learn-pages-router/basics/api-routes/creating-api-routes

I placed the 'hello.ts' file under 'src/pages/api' and then under 'src/app/pages/api' and make the call with axios like this:

const dataAxios = axios.get('../pages/api/hello') // calling the file in the folder 'src/app/pages/api' .then( res => console.log('dataAxios - res: ', res) ).catch( err => console.error('dataAxios - error: ', err) ); useEffect(() => { console.log('Data images -- dataAxios -- from public static folder:', JSON.stringify(dataAxios)); }, [dataAxios]);
And i got this error: GET http://localhost:3000/pages/api/get-images-from-public-folder 404 (Not Found)

dataAxios - error: AxiosError {message: 'Request failed with status code 404', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}

Link in Github discussion: https://github.com/vercel/next.js/discussions/57924
Answered by @ts-ignore
- That's not how you call an internal api
- the pages is just a folder to put your source code and doesn't reflect int he url so the url will be /api/get-images-from-public-folder
View full answer

8 Replies

Answer
- 'use client' doesn't work in pages router
Cape horse mackerel
And use app router instead 🙂
@@ts-ignore - `'use client'` doesn't work in pages router
Yacare CaimanOP
I used the 'use client' ONLY for the front-end to render to view page (HTML page by React).
@Cape horse mackerel And use app router instead 🙂
Yacare CaimanOP
How is that ?
@Yacare Caiman I used the 'use client' ONLY for the front-end to render to view page (HTML page by React).
that's not the use of use client + as said earlier it doesn't work with pages router
Yacare CaimanOP
No, because when i delete it, i got this problem Maybe one of these should be marked as a client entry with "use client":