Axios call from a React app comes back 404
Request failed with status code 404Short 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.
Fix it fast
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); }2Match what you see, then jump to its fix
- 1
The 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
The 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
The 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
- 1
What does the error look like?
Request failed with status code 404404 (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/users2Fix
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/usersConfirmed by create-react-app.dev
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/about2Fix
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/usersConfirmed by expressjs.com
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.js2Fix
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/usersConfirmed by expressjs.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
Related fixes
Back end
React shows 'You need to enable JavaScript to run this app' with JavaScript on
fetch('/api/...') returns HTML instead of JSON
Seeing that sentence with JavaScript on means the HTML page loaded but your app did not start, or an API call got the HTML page back.
3 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
npm start stops with Could not find a required file in a React app
Name: index.html
react-scripts checks for two files before the dev server starts: public/index.html and a src/index file.
3 causes, each with a fix