Coder Social home page Coder Social logo

handmade-b's Introduction

前言

此專案為 MFEE27 資策會前端工程師就業養成班 HANDMADE 後端,使用 Node.js,啟動專案請先依照 .env.example 設定好 .env 檔案並安裝好套件

HANDMADE 前端連結

專案環境

  • Git : 2.37.0.widows.1
  • Node : 16.16.0
  • Mysql2 : ^2.3.3
  • Socket.io : ^4.5.2
  • Googleapis : ^107.0.0
  • @Google-cloud/local-auth : ^2.1.0

架構

server : 統整所有 server 程式碼的地方

router : 主程式(server.js) 與邏輯 (controller) 溝通的橋樑

controller : 與資料庫溝通(需要邏輯也寫這裡)

server.js 檔紀錄各類別路由

  • restful API 主體(ex./api/user)
  • 第一個參數:要到達的路由
  • 第二個參數:router
app.use('/api/product', productRouter)
app.use('/api/course', courseRouter)
app.use('/api/store', storeRouter)

routers 資料夾存放各細項路由及 api 類型

  • restful API 動詞( get , post, put , delete )
  • 第一個參數:要到達的路由
  • 第二個參數:call back 函式 (放入 controller export 出來的函式)
router.get('/:storeId', getStoreCourse)
router.get('/', getAllCourse)
router.post('/:courseId', addFavoriteCourseTable)
router.delete('/:courseId', removeFavoriteCourseTable)
router.get('/detail/:courseId', getCourseDetail)
router.get('/comment/:courseCommentId', getCourseComment)

module.exports = router

controllers 資料夾存放 api 方法

  • 在 controllers 資料夾中新增 reply-controller.js 檔案,裡面寫與 db 溝通的 sql 語法(記得引入資料庫),最後要 export 出寫好的 function。
const getStoreCourse = async (req, res) => {
  const storeId = req.params.storeId
  const [data] = await pool.execute(
    'SELECT course.*,store.name AS store_name, category.category_en_name FROM course JOIN store ON course.store_id = store.id JOIN category ON category.id = course.category_id WHERE course.store_id =?',
    [storeId]
  )

  const [courseImgs] = await pool.execute('SELECT course_img.*,course.store_id FROM course_img JOIN course ON course.id = course_img.course_id')

  const reaponse = data.map((v) => {
    const course_img = courseImgs.filter((img) => img.course_id === v.id)
    const imgName = course_img.map((img) => img.img_name)
    v['imgName'] = imgName
    return v
  })

  res.json(reaponse)
}

Backend

handmade-b

handmade-b's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.