0
0
ExpressDebug / FixBeginner · 4 min read

How to Fix 'Cannot GET /route' Error in Express

The Cannot GET /route error in Express happens when the requested route is not defined or middleware order is wrong. To fix it, ensure you have a matching app.get() or other HTTP method route handler for the path, and that it is registered before any catch-all middleware.
🔍

Why This Happens

This error occurs because Express does not find a route handler matching the requested URL and HTTP method. It usually means you forgot to define the route, misspelled the path, or placed the route handler after middleware that ends the request.

javascript
import express from 'express';
const app = express();

// Missing route for '/hello'

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
Output
GET /hello 404 Not Found Cannot GET /hello
🔧

The Fix

Define the route handler for the requested path using app.get() or the appropriate HTTP method. Also, make sure this route is registered before any middleware that sends a response or handles 404 errors.

javascript
import express from 'express';
const app = express();

app.get('/hello', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
Output
Server running on port 3000 When you visit http://localhost:3000/hello in a browser, it shows: Hello World!
🛡️

Prevention

Always define routes explicitly for each path and HTTP method you want to support. Use consistent naming and check for typos. Register routes before any catch-all middleware like 404 handlers. Use tools like linters or TypeScript to catch missing routes early.

⚠️

Related Errors

404 Not Found: Happens when no route matches the request.

Method Not Allowed: Occurs if the route exists but the HTTP method (GET, POST, etc.) is not handled.

Quick fixes include adding the missing route or supporting the correct HTTP method.

Key Takeaways

Define route handlers for every path and HTTP method you want to serve.
Register routes before middleware that ends requests or handles 404 errors.
Check for typos in route paths and HTTP methods.
Use linters or TypeScript to catch missing or incorrect routes early.
Understand Express middleware order affects route matching.