如何将两条不同的路由映射到同一个路由器

How to map two different routes to the same Router

我在 server.js 有两条路线:

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

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;

如何将请求 /credit/success 引用到 success 函数,以及 请求 /credit/fail 到路由器中的 fail 函数?

只需更新下面提到的server.js中间件用法:

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

然后就可以通过访问ie来访问终点

/credit/success
/credit/fail

在 server.js 中你需要一行:

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

这将添加到“/credit”路径中,无论您在 credit-handler 中有什么