Mark
Mark

Reputation: 53

The router file doen't get return result on koajs, nodejs

I got puzzled on the router on koajs, e.g. there're two files, one is the main file app.js, code like following

//some codes
const home = require("./router/home.js");
router.use("/", home.routes());

and another is router file home.js, code like following

router
    .get("/:rel", async (ctx) => {
        console.log("run sub");
    })
    .get("/", async (ctx) => {
        console.log("run root");
    })
module.exports = router;

I thought http://localhost:3000/ can return the console log run root, that's right, and http://localhost:3000/dlkjf can return run sub, but it's not, there's no return result, if I update home.js to

 router
    .get("/:rel", async (ctx) => {
        console.log("run sub");
    })
module.exports = router;

Still got nothing, any ideas? Thanks.

Upvotes: 1

Views: 58

Answers (1)

El.
El.

Reputation: 1245

It's about module.exports property that you set its value at the end of the home.js:

module.exports = router;

you should pass the router like this:

module.exports = {router};

or at app.js use it as follow:

const routes = require("./router/home.js");
router.use("/", routes());

Upvotes: 2

Related Questions