使用AngularJS、Node.js及MySQL时遭遇Cannot GET错误,请求排查
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 yourindex.htmllives. Ifindex.htmlis in your project root, usepath.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.
- Replace
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.htmlis spelled correctly (no uppercase letters likeIndex.htmlunless that’s exactly how the file is named). - On Linux/macOS, file paths are case-sensitive—if your server looks for
public/Index.htmlbut the file ispublic/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

