I use koa to create a nodejs application, and I try to optimize.
app.js
'use strict';
const Koa = require("koa");
const Router = require("koa-router");
const router = new Router();
const stuff = require("./router/stuff.js");
router.use("/stuff", stuff.routes());
//more routers like above
const home = require("./router/home.js");
router.use(home.routes());
app.use(router.routes(), router.allowedMethods());
app.listen(port);
There’re some router files in folder router
, such as stuff.js
, home.js
, client.js
stuff.js
'use strict';
const router = require("koa-router")();
router
.get("/", async (ctx) => {
await ctx.render("stuff", {
});
})
module.exports = router;
home.js
'use strict';
const router = require("koa-router")();
router
.get("/", async (ctx) => {
await ctx.render("home", {
});
})
module.exports = router;
client.js
'use strict';
const router = require("koa-router")();
router
.get("/", async (ctx) => {
await ctx.render("client", {
});
})
module.exports = router;
I think the way I wrote is still very amateurish, how to optimize it? Thank you.