In defining route, we have plenty of choices - we could put all routes in single file and import it into the entry point, or we could create a main route and many sub-routes in different files and import all sub-routes into the main route that will be imported into the entry point. In fact, we could define two routes to the dashboard job and category pages as below:

 

//route/admin.js
import express from 'express'
const adminRoute = express.Router()

import jobRoute from './admin/job.js'
adminRoute.use('/job',jobRoute)

import categoryRoute from './admin/category.js'
adminRoute.use('/category',categoryRoute)

export default adminRoute

 

//route/admin/job.js
import express from 'express'
const jobRoute = express.Router()
import job from '../../controller/admin/job.js'

jobRoute.get('/',async (req,res,next)=>{
    if(req.session.user){
        job.getItem(req,res)
    }else{
        res.redirect('/login')
    }
})

export default jobRoute

 

// controller/admin/job.js
import config from '../../config.js'

class Job{
    constructor(){
        (async () => {
            this.config = await config()
        })()
    }

    getItem(req,res){
        this.config.pageTitle = 'ទំព័រ​ការងារ'
        this.config.route = '/admin/job'
        this.config.type = 'job'

        res.render('base',{data: this.config})
    }
}

export default await new Job()

 

// route/admin/category.js
import express from "express"
const categoryRoute = express.Router()
import category from '../../controller/admin/category.js'

categoryRoute.get('/',async (req,res,next) => {
    if(req.session.user){
        category.getItem(req,res)
    }else{
        res.redirect('/login')
    }
})

export default categoryRoute

 

// controller/admin/category.js
import config from '../../config.js'

class Category{
    constructor(){
        (async () => {
            this.config = await config()
        })()
    }

    getItem(req,res){
        this.config.pageTitle = 'ទំព័រ​ជំពូក'
        this.config.route = '/admin/category'
        this.config.type = 'category'

        res.render('base',{data:this.config})
    }
}

export default await new Category()

 

GitHub: https://github.com/Sokhavuth/khmerweb-job

Heroku: https://khmerweb-job.herokuapp.com