๐ŸŒ Detecting your locationโ€ฆ

How to Fix Cannot GET Error in Express.js: Complete Solution Guide

โฑ๏ธ5 min read  ยท  894 words

The error Cannot GET / (or Cannot GET /some-path) in the browser when hitting your Express server means Express received the request but has no route handler for that path and method. Here’s how to fix every cause.

What This Error Means

“Cannot GET /path” is Express’s default 404 response โ€” it received a GET request for /path but found no matching route. The request reached your server (good), but no route handles it. The fix is defining the right route or serving the right files.

Cause 1: No Route Defined for the Path

// ๐Ÿ› Server has no route for the root path
const express = require('express');
const app = express();
app.listen(3000);
// Visiting http://localhost:3000/ โ†’ "Cannot GET /"

// โœ… Define a route for /
app.get('/', (req, res) => {
  res.send('Hello World');
});
app.listen(3000);

Cause 2: Wrong HTTP Method

// ๐Ÿ› Route is POST but you're making a GET request
app.post('/users', createUser);
// Browsing to /users (a GET) โ†’ "Cannot GET /users"
// (the POST route exists, but not a GET route)

// โœ… Add a GET route if you need one
app.get('/users', getUsers);    // for GET requests
app.post('/users', createUser); // for POST requests

Cause 3: Serving Static Files Incorrectly

// ๐Ÿ› Expecting index.html to serve but static middleware isn't set up
// Visiting / โ†’ "Cannot GET /" because there's no route or static config

// โœ… Serve static files (HTML, CSS, JS) from a directory
app.use(express.static('public'));
// Now files in ./public are served: /index.html at /, /style.css, etc.

// โœ… Or serve a specific file at the root
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/public/index.html');
});

Cause 4: Route Defined After app.listen or Middleware Order

// ๐Ÿ› Routes defined AFTER listen() never register
app.listen(3000);
app.get('/', handler);   // โŒ too late - server already started

// โœ… Define routes BEFORE listen()
app.get('/', handler);
app.listen(3000);

// ๐Ÿ› A catch-all or 404 middleware BEFORE your routes intercepts everything
app.use((req, res) => res.status(404).send('Not found'));
app.get('/users', getUsers);   // โŒ never reached - 404 middleware ran first

// โœ… Put the 404 handler LAST
app.get('/users', getUsers);
app.use((req, res) => res.status(404).send('Not found'));   // last

Cause 5: Trailing Slash or Path Mismatch

// ๐Ÿ› Route path doesn't exactly match the request
app.get('/users', getUsers);
// Request to /users/ (trailing slash) may not match depending on config

// โœ… Express matches /users and /users/ by default in most versions,
// but be consistent. For explicit control:
app.get('/users', getUsers);
// Or handle both:
app.get(['/users', '/users/'], getUsers);

Cause 6: Router Not Mounted Correctly

// ๐Ÿ› Router defined but not mounted, or wrong prefix
const router = express.Router();
router.get('/list', getList);
// Forgot to mount it, or mounted at wrong path

// โœ… Mount the router with a base path
const usersRouter = express.Router();
usersRouter.get('/list', getList);
app.use('/users', usersRouter);
// Now the route is at /users/list, not /list

Debugging: See Your Registered Routes

// List all registered routes to verify what exists
app._router.stack
  .filter(layer => layer.route)
  .forEach(layer => {
    const methods = Object.keys(layer.route.methods).join(', ').toUpperCase();
    console.log(`${methods} ${layer.route.path}`);
  });
// Prints: GET /, POST /users, etc.
// Confirms whether the route you expect actually exists

Proper 404 Handling

// Define all your routes first
app.get('/', homeHandler);
app.get('/users', getUsers);
app.post('/users', createUser);

// Then a catch-all 404 handler LAST (nicer than default "Cannot GET")
app.use((req, res) => {
  res.status(404).json({ error: `Route not found: ${req.method} ${req.path}` });
});

Frequently Asked Questions

Q: Why do I get “Cannot GET /” on the root path?
A: You haven’t defined a route for /. Add app.get('/', (req, res) => res.send('Hello')) or serve static files with app.use(express.static('public')) so index.html loads at the root.

Q: My POST route works but GET gives Cannot GET. Why?
A: You defined the route with app.post() but are making a GET request (e.g., typing the URL in a browser, which sends GET). Add an app.get() route for that path if you need to handle GET requests too.

Q: How do I serve an HTML page at the root?
A: Use app.use(express.static('public')) to serve a directory (index.html loads at /), or app.get('/', (req, res) => res.sendFile(__dirname + '/index.html')) for a specific file.

Q: Why does my router’s routes give Cannot GET?
A: You may not have mounted the router (app.use('/prefix', router)), or the full path is different than expected. A router mounted at /users with a route /list is accessed at /users/list, not /list. Check the combined path.

Q: Does route order matter in Express?
A: Yes โ€” Express matches routes top to bottom. A catch-all or 404 middleware must come LAST, after all specific routes, or it intercepts requests before they reach your routes. Order your routes from specific to general.

Conclusion

“Cannot GET /” means Express received the request but has no matching route for that path and method. The fixes: define a route for the path (app.get('/', ...)), match the correct HTTP method, serve static files with express.static(), define routes before app.listen(), and put 404 handlers last. If it’s a router, ensure it’s mounted with the right prefix. When stuck, log your registered routes to confirm what actually exists. Once you verify the route is defined, mounted correctly, and matches the request method and path, the error resolves.

MD Rafikul Islam

Written by

MD Rafikul Islam is a software developer and the editor of TechPulse. He writes about developer tooling, hardware, and the practical decisions that come up in day-to-day engineering work โ€” which laptop to buy, which framework to commit to, why a build broke at 2am. He tests the tools he writes about and says plainly when something is not worth the money. Corrections and corrections requests are welcome at rony.yf25@gmail.com.

โœ๏ธ Leave a Comment

Your email address will not be published. Required fields are marked *

๐ŸŒ Read in:๐Ÿ‡ฌ๐Ÿ‡ง English๐Ÿ‡ฉ๐Ÿ‡ช Deutsch๐Ÿ‡ง๐Ÿ‡ท Portuguรชs๐Ÿ‡ธ๐Ÿ‡ฆ ุงู„ุนุฑุจูŠุฉ๐Ÿ‡ฎ๐Ÿ‡ณ เคนเคฟเคจเฅเคฆเฅ€๐Ÿ‡ง๐Ÿ‡ฉ เฆฌเฆพเฆ‚เฆฒเฆพ