Node.js开发报错Cannot GET /:待办事项应用路由与视图问题求助
Cannot GET / Error in Your Todo List App Hey there! Let's work through this Cannot GET / error together—it's one of the most common hiccups with Express + EJS apps, so we'll get it sorted out quickly. Based on your hunch about missing routes and view bugs, here's a step-by-step breakdown to diagnose and fix the issue:
1. Verify Your Root Route Configuration
First up, let's make sure your app actually has a route handler for the root path (/).
Check app.js for Route Setup
Open your app.js file and confirm two key things:
- You've set EJS as your view engine (required to render
.ejsfiles):app.set('view engine', 'ejs'); - You either defined a root route directly in
app.js, or mounted a router file that handles the root path. For example:// Option 1: Direct root route in app.js app.get('/', (req, res) => { // Pass any necessary data to your view (like an empty todos array) res.render('index', { todos: [] }); }); // Option 2: Mount a router (e.g., todo.js) to the root path const todoRoutes = require('./routes/todo'); app.use('/', todoRoutes);
If you went with Option 2, open routes/todo.js and make sure it has a root route handler:
const router = require('express').Router(); router.get('/', (req, res) => { res.render('index', { todos: [] }); }); module.exports = router;
Watch Middleware Order
Make sure your route definitions come before any 404 or error-handling middleware. If you have something like this in app.js:
// ❌ Wrong order: 404 middleware blocks routes app.use((req, res) => { res.sendStatus(404); }); app.use('/', todoRoutes);
Swap them so routes run first:
// ✅ Correct order app.use('/', todoRoutes); app.use((req, res) => { res.sendStatus(404); });
2. Debug Your index.ejs View
If your route looks solid, let's check the view file next.
Confirm File Location & Spelling
Double-check that index.ejs is in the views folder (Express looks here by default) and that the filename is spelled correctly (case matters on Linux/macOS—Index.ejs won't work if you're calling res.render('index')).
Simplify to Test for Syntax Bugs
Sometimes a broken EJS tag can cause rendering failures (though this usually throws a 500 error, not a 404). To rule this out, replace your index.ejs with a minimal working version:
<!DOCTYPE html> <html> <head> <title>My Todo List</title> </head> <body> <h1>Hello from the Todo App!</h1> </body> </html>
Restart your server and try accessing http://localhost:3000 (or whatever port you're using). If this loads, the issue was a syntax bug in your original index.ejs—gradually add back your original code to find the problematic line.
3. Quick Sanity Checks
- Make sure your server is running without errors. Check your terminal for any startup messages—if there's a syntax error in
app.jsor your routes, the server might not start properly. - Confirm you're accessing the correct URL. If your server is listening on port 5000, you need to go to
http://localhost:5000, not port 3000.
Once you work through these steps, you should have a better idea where the problem lies. If you're still stuck, sharing snippets of your app.js and route files would help narrow it down further!
内容的提问来源于stack exchange,提问作者leon

