JAN
JAN

Reputation: 21885

How to map two different routes to the same Router

I have two routes in server.js :

  app.use("/credit/success", require("./credit/credit-handler"));
  app.use("/credit/fail", require("./credit/credit-handler"));

And credit-handler :

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

// @route  GET credit success
// @desc   Get credit success screen
// @access Public

router.get("/success", async (req, res) => {
  try {
    res.sendFile("./credit/SuccessTransaction.html");
  } catch (err) {
    console.error(err.message);
    res.status(500).send("Server Error :" + err.message);
  }
});

// @route  GET credit failure
// @desc   Get credit fail screen
// @access Public

router.get("/fail", async (req, res) => {
  try {
    res.sendFile("./credit/FailTransaction.html");
  } catch (err) {
    console.error(err.message);
    res.status(500).send("Server Error :" + err.message);
  }
});

module.exports = router;

How can I refer the request /credit/success to the success function , and the request /credit/fail to the fail function in the router ?

Upvotes: 0

Views: 67

Answers (2)

Andrei Panturu
Andrei Panturu

Reputation: 109

In server.js you will need a single line:

app.use("/credit", require("./credit/credit-handler"));

That will add to the '/credit' path whatever you have in the credit-handler

Upvotes: 1

KrishnaSingh
KrishnaSingh

Reputation: 746

Just update the server.js middleware usage mentioned below:

app.use("/credit", require("./credit/credit-handler"));

Then you can access the end point by accessing i.e

/credit/success
/credit/fail

Upvotes: 1

Related Questions