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

使用AngularJS、Node.js及MySQL时遭遇Cannot GET错误,请求排查

Troubleshooting "Cannot GET" Error with AngularJS + Node.js + MySQL Setup

Hey there, let's work through this "Cannot GET" error step by step—it’s a super common hiccup with Node.js + frontend setups, so we’ll get your app up and running in no time.

1. Make Sure Your server.js Serves Static Files

Node.js doesn’t automatically serve your frontend files (like index.html or AngularJS’s app.js). If you’re using Express (the standard for Node.js servers), you need to add middleware to handle static assets. Here’s what that should look like:

const express = require('express');
const app = express();
const path = require('path');

// Serve static files from your frontend directory (adjust the path to match your setup)
app.use(express.static(path.join(__dirname, 'public'))); 

// Catch-all route for AngularJS client-side routing (critical for SPAs)
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

const PORT = 8080;
app.listen(PORT, () => {
  console.log(`Server live at http://localhost:${PORT}`);
});
  • Quick Checks:
    • Replace 'public' with the actual folder where your index.html lives. If index.html is in your project root, use path.join(__dirname) instead.
    • The app.get('*') route ensures that even if you use AngularJS’s routing, refreshing the page or accessing deep routes won’t throw errors—but it also fixes the root path issue you’re seeing now.

2. Verify Your Folder Structure & File Paths

Double-check that your files are arranged correctly relative to server.js. A typical setup looks like this:

your-project/
├── server.js
├── public/
│   ├── index.html
│   ├── app.js (your AngularJS application)
│   └── css/
└── package.json

If your index.html is in the root folder instead of public, update the express.static and sendFile paths to use just __dirname instead of path.join(__dirname, 'public').

3. Check Your package.json start Script

Ensure your npm start command is actually running your server. Open package.json and confirm the scripts section looks like this:

"scripts": {
  "start": "node server.js"
}

If it’s pointing to app.js (your AngularJS file) instead of server.js, your Node server won’t start at all—that’s a quick fix!

4. Test If the Server Is Responding At All

To rule out server-level issues, add a simple test route to server.js:

app.get('/test', (req, res) => {
  res.send('Server is working!');
});

Restart the server with npm start and visit localhost:8080/test. If you see the message, your server is running, and the problem is with static file serving. If not, check your terminal for error messages when starting the server—there might be a syntax error or MySQL connection issue blocking it.

5. Watch for Typos & Case Sensitivity

  • Make sure index.html is spelled correctly (no uppercase letters like Index.html unless that’s exactly how the file is named).
  • On Linux/macOS, file paths are case-sensitive—if your server looks for public/Index.html but the file is public/index.html, it’ll fail.

6. Ensure MySQL Isn’t Blocking the Server

If your server.js tries to connect to MySQL on startup and fails (wrong credentials, MySQL not running, etc.), it might never reach the app.listen() line. Check your terminal output when running npm start for any MySQL-related errors. Fix those first, then test the server again.

Once you’ve worked through these steps, you should be able to access localhost:8080 or localhost:8080/index.html without the "Cannot GET" error. If you hit a specific code-related snag, share snippets of your server.js or folder structure, and we’ll dig deeper!

内容的提问来源于stack exchange,提问作者Raghu Venkataa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:42:18