0

I am using AngularJS with NodeJS as backend. I have 2 route files - route1.api.js and route2.api.js. They are used in server.js as follows:

const route1Api= require("./src/api/route1.api");
const route2Api= require("./src/api/route2.api");

app.use("/api/route1/", route1Api);
app.use("/api/route2/", route2Api);

route1.api.js and route2.api.js have similar code like the following:

const router = express.Router();
router.post("/whatever", (req, res) => {
// Code
});
module.exports = router;

I am accessing these routes from 2 different Angular data service files:

In route1.data.service.js

postRoute1() {
    console.log("Route1");
    return this._http
      .post("/api/route1/whatever", postData)
      .map(result => console.log(result));
}

In route2.data.service.js

postRoute2() {
    console.log("Route2");
    return this._http
      .post("/api/route2/whatever", postData)
      .map(result => console.log(result));
}

Now, the problem is that route1 is working fine, but route2 isn't. The postRoute2() method works only up to the console.log() line but not beyond that. What could be the problem?

route2.api.js code:

router.post("/whatever", (req, res) => {
  console.log("API hit");
  User.find({}, function(err, user) {
    if (err) res.status(500);
    if (!user) res.status(204);
    res.status(200).json(user);
  });
});
Clairvoyant
  • 81
  • 1
  • 9

1 Answers1

0

Modify the postRoute2() method as:

postRoute2() {
    console.log("Route2");
    return this._http
      .post("/api/route2/whatever", postData)
      .subscribe(result => console.log(result));
}
4b0
  • 21,981
  • 30
  • 95
  • 142
Clairvoyant
  • 81
  • 1
  • 9