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

import loginRoute from './admin/login.js'
admin.use('/login', loginRoute) 

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

import postRoute from './admin/post.js'
admin.use('/post', postRoute) 

import bookRoute from './admin/book.js'
admin.use('/book', bookRoute)

import uploadRoute from './admin/upload.js'
admin.use('/upload', uploadRoute)
 
export default admin

 

//routes/admin/upload.js
//npm install multer
import path from 'path'
import express from 'express'
const uploadRoute = express.Router()

uploadRoute.get('/',async function(req,res){
    if(req.session.user){
        const module = await import('../../controllers/admin/upload/get.js')
        module.default(req,res)
    }else{
        res.redirect('/admin/login')
    }
})

export default uploadRoute

 

//controllers/admin/upload/get.js
import settings from '../../../settings.js'

let mySettings = JSON.parse(JSON.stringify(settings))

export default async (req,res)=>{
    mySettings.pageTitle = 'ទំព័រ​ Upload'
    mySettings.route = '/admin/upload'

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

 

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

    <div class='Upload'>
        <form action='/admin/upload' method='post' enctype="multipart/form-data">
            <input type='file' name='uploadFile' required />
            <input type='text' value='<%= data.fileUrl %>' 
                placeholder='តំណរភ្ជាប់ឯ​កសារ​នៅ​លើ Server' />
            <input type='submit' value='បញ្ជូន​ទិន្នន័យ' />
        </form>
    </div>

 

/* public/styles/admin/upload.css */
.Upload form{
    display: grid;
    grid-template-columns: 50%;
    grid-gap: 3px;
}

.Upload form input{
    font: var(--body-font);
    padding: 2px 5px;
}

.Upload form input[type="file"]{
    padding: 0;
    color: black;
    background: white;
}

 

GitHub: https://github.com/Sokhavuth/Blog_

Vercel: https://khmerweb-blog.vercel.app/