Зачем нужны роутеры если можно обходиться без них в express.js?

Приведу пример: index.js:

const express = require("express")
const router = require("./routes/index")

const app = express()

app.use("/api", router)

app.listen(3000)

router(./routes/index.js):

const express = require("express")
const router = new express()

router.post("/", )
router.get("/hello", (req, res) => {
    res.send("hello")
})

module.exports = router

Переходим по localhost:3000/api/hello и видим выдачу нашего "hello". НО, мы можем провернуть тоже самое с объектом Router(), чуть изменим наш router:

const express = require("express")
const router = new express.Router() // добавим сам объект Router()

router.post("/", )
router.get("/hello", (req, res) => {
    res.send("hello")
})

module.exports = router

И опять переходим на localhost:3000/api/hello и видим тоже самое "hello" Собственно вопрос, а в чем тогда разница?


Ответы (0 шт):