Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Axios call from a React app comes back 404

ErrorRequest failed with status code 404

Short answer

Status code 404 means a server answered, but it has nothing at that path. Log error.config.url and the base URL first: that is the address axios really called. Usually the path is wrong, the request went to the React dev server instead of your API, or the API has no route for that method and path.

  • Applies to axios 1.x, React 19, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the address axios actually used and the status line. Compare it character by character with the route your server defines.

    try {
      await axios.get('/api/users');
    } catch (error) {
      console.log(error.config.baseURL, error.config.url, error.response?.status);
    }
  2. 2Match what you see, then jump to its fix

    1. 1The failing URL starts with the dev server's own origin (for example http://localhost:3000) and the server answering is not your APIThe request went to the React dev server, not to your APIFix
    2. 2The route exists in the code, but curl to the exact URL axios used still returns 404The path is mounted under a prefix the client leaves outFix
    3. 3The same path works with another HTTP method, or the route is registered after the 404 catch-allThe server has no handler for that method and pathFix

What does the error look like?

  • Request failed with status code 404
  • 404 (Not Found)

Cause 1 of 3

The request went to the React dev server, not to your API

1Check

A relative URL such as /api/users is sent to the page's own origin. In development, Create React App forwards a request to the API only when the proxy field is set and the request does not ask for text/html. The proxy has effect in development only.

grep -n '"proxy"' package.json
curl -i http://localhost:4000/api/users

2Fix

Point the proxy field at the port where the API really listens, or put the full API address in an axios instance so no proxy is needed.

// package.json
"proxy": "http://localhost:4000"

// or src/api.js
import axios from 'axios';
export const api = axios.create({ baseURL: 'http://localhost:4000' });

3Verify

Repeat the call and check that the Network tab shows the API's address and a 2xx status.

curl -i http://localhost:4000/api/users

Confirmed by create-react-app.dev

Did this fix it?

Cause 2 of 3

The path is mounted under a prefix the client leaves out

1Check

In Express a router is mounted with a prefix, and its own paths are relative to that prefix. A router with '/about' mounted at '/birds' answers at '/birds/about', not '/about'.

grep -n "app.use(" server.js
curl -i http://localhost:4000/birds/about

2Fix

Make the client URL equal prefix plus route path, or change the prefix in app.use. Keep the prefix in one place on the client by using the baseURL of an axios instance.

// server
app.use('/api', usersRouter);   // usersRouter has router.get('/users', ...)

// client
const api = axios.create({ baseURL: 'http://localhost:4000/api' });
await api.get('/users');

3Verify

The same request returns the data instead of 404.

curl -i http://localhost:4000/api/users

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 3

The server has no handler for that method and path

1Check

Express matches routes per HTTP verb: app.get handles GET only, app.post handles POST only. A catch-all 404 middleware must be registered after every route, because Express runs handlers in order and answers 404 only when none responded.

curl -i -X POST http://localhost:4000/api/users
grep -n "app.get\|app.post\|status(404)" server.js

2Fix

Register a handler for the verb and path the client uses, and move the 404 middleware to the very end of the file.

app.post('/api/users', createUser);
// ... all other routes above this line
app.use((req, res) => {
  res.status(404).send('Not found');
});

3Verify

Send the failing request again with the same method.

curl -i -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:4000/api/users

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Create one axios instance with baseURL and import it everywhere, so a path is never built by hand in two places.
  • Log error.config.url in a shared response interceptor so every 404 shows the real address.
  • Keep the route list of the API in one file and compare it with the client calls when you rename a path.
  • Test new endpoints with curl before wiring them to the UI.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.