In the last tutorial, after we successfully signed into the future dashboard, we were directed through the route  “/admin/job” that does not exist yet. So, we need to build this route first before building a dashboard.

 

// index.js
// npm install express
// npm install express-session
// npm install connect-mongo

import express from 'express'
const app = express()

import mydb from './model/conMongoDB.js'
app.use('/',async function(req,res,next){
    req.mydb = await mydb
    next()
})

import dotenv from 'dotenv'
dotenv.config()
import session from 'express-session'
import MongoStore from 'connect-mongo'
app.use(session({
  store: MongoStore.create({mongoUrl:process.env.DATABASE_URI}),
  secret: process.env.SECRET_KEY,
  resave: false,
  saveUninitialized: false
}))

import path from 'path'
const __dirname = path.resolve()
app.set('views', path.join(__dirname, 'view'))
app.set('view engine', 'ejs')
app.use(express.static(path.join(__dirname, 'static')))
app.use(express.urlencoded({extended:false}))

import indexRoute from './route/index.js'
app.use('/',indexRoute)
import loginRoute from './route/login.js'
app.use('/login',loginRoute)
import adminRoute from './route/admin.js'
app.use('/admin',adminRoute)

const port = process.env.PORT || 8000
app.listen(port, () => {
  console.log(`This app is listening to the port ${port}`)
})

export default app

 

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

jobRoute.get('/job',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'

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

export default await new Job()

 

<!--view/base.ejs-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title><%= data.siteTitle %> | <%= data.pageTitle %></title>
        <link href="/images/siteLogo.png" rel="icon" />
        <link href="/styles/base.css" rel="stylesheet" />
        <link href="/fonts/setup.css" rel="stylesheet" />
        <script src="/scripts/jquery.js"></script>
    </head>
    <body>
        <% if(data.route === '/'){ %>
            <%- include('front/home.ejs') %>
        <% }else if(data.route === '/login'){ %>
            <%- include('front/login.ejs') %>
        <% }else if(data.route.includes('/admin')){ %>
            <%- include('admin/index.ejs') %>
        <% } %>
    </body>
</html>

 

<!--view/admin/index.ejs-->
<link rel="stylesheet" href="/styles/admin/index.css" />

Admin Page

 

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

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