HTTPError.net

The fastest way to diagnose, understand, and fix any HTTP status code

Node.js

How to Fix 404 Not Found Errors in Node.js Applications

Quick Fix

The most common cause of 404 errors in Node.js is: Route not defined in Express/Fastify

Quick solution: Define the route in your Express app:

A 404 Not Found error in Node.js is usually caused by one of a handful of configuration problems. Work through the causes below in order - the first ones are the most common.

Common Causes in Node.js

Step-by-Step Solutions

Solution 1: Add Route Handler (Express)
  1. Define the route in your Express app:
  2. app.get('/your-path', (req, res) => { res.send('Hello'); });
  3. Check route order - specific routes before wildcards
  4. Verify HTTP method matches (GET, POST, etc.)
  5. Test: curl -X GET http://localhost:3000/your-path
Solution 2: Configure Static File Serving
  1. Add express.static middleware:
  2. app.use(express.static('public'));
  3. Or with virtual path: app.use('/static', express.static('public'));
  4. Ensure files exist in the public directory
  5. Check file permissions: ls -la public/
Solution 3: Add 404 Error Handler
  1. Add catch-all route after all other routes:
  2. app.use((req, res, next) => {
  3. res.status(404).send('404 - Not Found');
  4. });
  5. Or serve custom 404 page:
  6. res.status(404).sendFile(__dirname + '/views/404.html');
Solution 4: Debug Route Registration
  1. Express 4: log registered routes with app._router.stack.forEach(r => { if (r.route) console.log(r.route.path); }); (internal API, changed in Express 5)
  2. Use express-list-routes package: npm install express-list-routes
  3. const listRoutes = require('express-list-routes'); listRoutes(app);
  4. Check if your route appears in the output
Solution 5: Check Case Sensitivity
  1. Express routing is case-insensitive by default ('case sensitive routing' is off)
  2. If someone enabled it, check: app.get('case sensitive routing')
  3. Static files are different: on Linux, express.static matches file names case-sensitively
  4. Ensure file and folder names match the URL exactly before deploying to Linux

Prevention Tips