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.
๐ Table of Contents
- What This Error Means
- Cause 1: No Route Defined for the Path
- Cause 2: Wrong HTTP Method
- Cause 3: Serving Static Files Incorrectly
- Cause 4: Route Defined After app.listen or Middleware Order
- Cause 5: Trailing Slash or Path Mismatch
- Cause 6: Router Not Mounted Correctly
- Debugging: See Your Registered Routes
- Proper 404 Handling
- Frequently Asked Questions
- Conclusion
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.
๐ You might also like
๐ Share this article




โ๏ธ Leave a Comment