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

NodeJS API本地运行正常但部署至Heroku后仅根路由可访问,其余路由返回404问题求助

Troubleshooting 404 Errors on Heroku-Deployed Node.js API (Except "/" Route)

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.json scripts: Make sure you have a start script pointing to your entry file:
    "scripts": {
      "start": "node app.js",
      ...
    }
    
    If you skip the Procfile, Heroku uses the start script 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 status locally to check if any route files are listed as "untracked".
  • If so, add them with git add routes/, commit with git 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 config in your terminal to see the full list. Ensure MYSQL_REMOTE_USER and MYSQL_REMOTE_PASS match your local setup.
  • Double-check the hardcoded host and database values 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 15:17:33