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:

The 404 Not Found error in Node.js indicates a problem specific to your Node.js configuration or environment. This guide provides platform-specific solutions.

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. Log all registered routes: app._router.stack.forEach(r => { if (r.route) console.log(r.route.path); });
  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. Routes are case-sensitive by default
  2. Enable case-insensitive routing: app.set('case sensitive routing', false);
  3. Or normalize URLs in middleware
  4. Ensure file paths match exactly on Linux

Prevention Tips