//routes/backend.js

import express from 'express'
const backendRouter = express.Router()

import loginRouter from './backend/login.js'
backendRouter.use('/login', loginRouter) 

import categoryRouter from './backend/category.js'
backendRouter.use('/category', categoryRouter)
 
export default backendRouter

 

//routes/backend/category.js

import express from 'express'
const categoryRouter = express.Router()
import category from '../../controllers/backend/category.js'

categoryRouter.get('/', async function(req,res){
    await category.getPage(req,res)
})

export default categoryRouter

 

//controllers/backend/category.js

class Category{
    async getPage(req,res){
        if(req.session.user){
            req.settings.pageTitle = 'ទំព័រ​ជំពូក'
            req.settings.message = ''
            req.settings.route = '/admin/category'
  
            res.render('base', {data:req.settings})
        }else{
            res.redirect('/admin/login')
        }
    }
}

export default new Category()

 

<!--views/backend/index.ejs-->

<link href="/styles/backend/index.css" rel="stylesheet">
 
<section class="Header">
    <div class="wrapper region">
        <div class="logo"><%= data.pageTitle %></div>
        <form method="post" action="/search">
            <select name="type">
                <option>ការផ្សាយ</option>
                <option>ទំព័រស្តាទិក</option>
                <option>អ្នក​ប្រើប្រាស់</option>
            </select>
            <input type="text" name="q" placeholder="Search" required>
            <input type="submit" value="ស្វែង​រក">
        </form>
        <div class="logout">
            <a href="/">ទំព័រ​មុខ</a> | <a href="/admin/login/logout">ចេញ​ក្រៅ</a>
        </div>
    </div>
</section>
 
<section class="Body">
    <div class="wrapper region">
        <div class="sidebar">
            <a href='/admin/post'><img src='/images/movie.png' /></a>
            <a href='/admin/post'>ការផ្សាយ</a>
            
            <a href='/admin/category'><img src='/images/category.png' /></a>
            <a href='/admin/category'>ជំពូក</a>
            
            <a href='/admin/book'><img src='/images/books.png' /></a>
            <a href='/admin/book'>សៀវភៅ</a>
            
            <a href='/admin/upload'><img src='/images/upload.png' /></a>
            <a href='/admin/upload'>Upload</a>
            
            <a href='/admin/user'><img src='/images/users.png' /></a>
            <a href='/admin/user'>អ្នក​ប្រើប្រាស់</a>
            
            <a href='/admin/setting'><img src='/images/setting.png' /></a>
            <a href='/admin/setting'>Setting</a>
        </div>
        
        <% if(data.route == '/admin/post'){ %>
            <%- include('./post.ejs') %>
        <% } else if(data.route == '/admin/category'){ %>
            <%- include('./category.ejs') %>
        <% } %>

    </div>
</section>

<section class="Footer region">
    <div class="info">សរុប​ទាំងអស់​មាន​ចំនួនៈ </div>

    <ul class="item"></ul>

    <div class="paginate"><img src='/images/load-more.png' /></div>
</section>

 

<!--views/backend/category.ejs-->

<link href="/styles/backend/category.css" rel="stylesheet"></link>
 
<section class='content'>
    <form action='/admin/category' method='post' >
        <a>ឈ្មោះជំពូកៈ</a><input type='text' name='label' required />
        <a>រូបសញ្ញាៈ</a><input type='text' name='thumb' required />
        <a>ពេល​បង្កើតៈ</a><input type='datetime-local' value='' name="datetime" required />
        <a></a><input type='submit' value='បញ្ជូនទិន្នន័យ' />
    </form>
</section>

 

/* public/styles/backend/category.css */

.content form{
    width: 60%;
    display: grid;
    grid-template-columns: 20% 80%;
    grid-gap: 5px;
    align-items: center;
}
 
.content form input{
    
}
 
.content form input[type=submit],
.content form input{
    font: var(--body-font);
    padding: 5px 10px;
}
 
.content form a{
    text-align: right;
    color: black;
}

 

GitHub: https://github.com/Sokhavuth/Node.js_backend

Vercel: https://vercel.com/sokhavuth/node-js-backend