You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js开发报错Cannot GET /:待办事项应用路由与视图问题求助

Troubleshooting the 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 .ejs files):
    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.js or 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:03:00