NodeJS API本地运行正常但部署至Heroku后仅根路由可访问,其余路由返回404问题求助
Hey there, let's figure out why only your / route works on Heroku while all others throw 404s—especially since everything runs smoothly locally. Here are the most likely culprits and how to fix them:
1. Missing or Misconfigured Procfile
Heroku relies on a Procfile to know exactly how to start your app. If it's missing or set up wrong, your API might fail to load all routes properly.
- Check for a Procfile: It should live in your project root (no file extension) with this line:
web: node app.js - Verify your
package.jsonscripts: Make sure you have astartscript pointing to your entry file:
If you skip the Procfile, Heroku uses the"scripts": { "start": "node app.js", ... }startscript by default—so this needs to be accurate.
2. Incorrect Router Export in Route Files
If your route files don't properly export an Express Router instance, Heroku won't recognize those endpoints. Double-check each route file (like students.js):
const express = require('express'); const router = express.Router(); // Example route router.get('/', (req, res) => { res.send('Student route works!'); }); // Critical: Make sure this line exists! module.exports = router;
Without module.exports = router;, app.use("/students", studentRouter) won't map any endpoints to that route.
3. Route Files Not Committed to Git
If you forgot to add your routes folder or individual route files to Git, Heroku won't deploy them at all.
- Run
git statuslocally to check if any route files are listed as "untracked". - If so, add them with
git add routes/, commit withgit commit -m "Add all route files", then redeploy to Heroku.
4. Environment Variable Checks (Less Likely for 404s, But Worth Confirming)
While 404s usually aren't database-related, a failed DB connection might break routes that depend on it (though that would typically throw 500 errors, not 404).
- Verify all environment variables are set correctly on Heroku: Run
heroku configin your terminal to see the full list. EnsureMYSQL_REMOTE_USERandMYSQL_REMOTE_PASSmatch your local setup. - Double-check the hardcoded
hostanddatabasevalues in your MySQL connection—make sure they're the correct public addresses from Clever Cloud (no typos!).
5. Dig Into Heroku Deployment Logs
The best way to spot hidden issues is to check Heroku's logs:
- Go to your Heroku app dashboard → Click "More" → "View logs".
- Look for red flags like:
- "Cannot find module './routes/teachers'" (means a route file is missing)
- "Error: connect ECONNREFUSED" (database connection failure)
- Any startup errors that prevent your app from loading all routes.
Quick Test to Confirm Your App is Running
To rule out static files overriding your / route, modify your indexRouter to return a unique message:
// In routes/index.js router.get('/', (req, res) => { res.send('My API is live on Heroku!'); });
Redeploy and visit /—if you see this message, your app is running, and the issue is definitely with how other routes are being loaded.
内容的提问来源于stack exchange,提问作者Shubh Ashish

