Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Express router working fine locally but not on heroku

My express app works fine on the localhost but it does not work on Heroku. When I added a line it stops working and

the line is

app.use("/api/product", require("./routes/product"))

Here is the code Index.js

const express = require("express");
const app = express();

const port = process.env.PORT || 5000;


app.get("/", (req, res) => {
    res.send("responded")
});

app.use(express.json())

app.use("/api/product", require("./routes/product"))


app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

product.js

const express = require("express");
const router = express.Router();

router.get("/", async (req, res) => {
    try {
        res.json({
            status: 200,
            message: "Data has been successfully fetched"
        });
    }
    catch (error) {
        console.log(error);
        return res.status(400).send("server error")
    }
})

module.exports = router;

package.json

{
  "name": "backend-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3"
  }
}

Folder structure

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You would wanna switch your route handlers place. Otherwise you will never rich your api, as the first catches all requests.

const express = require("express");
const app = express();

const port = process.env.PORT || 5000;
app.use(express.json())

app.use("/api/product", require("./routes/product"))
app.get("/", (req, res) => {
    res.send("responded")
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!