Route สำหรับ RESTful API ทั่วไป

Node & API by Keptcode.com

Last Updated: 2024-08-17 14:29

Repo: Github.com (รบกวนกด Start ให้ด้วยนะคับ)

https://github.com/coachmaxz/node-api

โครงสร้างโปรเจก (เพิ่มเติม)

Path: [skeleton]\node-api

Language: Project Structure

├── src/
│  ├── routes/
│  │  ├── api/
│  │  │  ├── example.api.js
│  │  │  └── index.js
│  │  └── index.js
└──└── index.js

แก้ไขไฟล์ index.js เพิ่ม Router เข้าไป

Path: [skeleton]\node-api\src\index.js

Language: Bash

// .....

// Express Configs
require('./app/express')(app)

// Routes
app.use(require('./routes')) // <= เพิ่มคำสั่ง (1/2)
app.use(express.static('public')) // <= เพิ่มคำสั่ง (2/2)

// .....

ไฟล์: index.js

[skeleton]/src/index.js

สร้างไฟล์ index.js กำหนด Path เป็น Version ต่าง ๆ

Path: [skeleton]\node-api\src\routes\index.js

Language: Bash

const router = require('express').Router()
const config = require('./../app/config')

// Example: /api/v1
router.use(`/api/v${config.apiVersion}`, require('./../routes/api'))

module.exports = router

ไฟล์: routes/index.js

[skeleton]/src/routes/index.js

สร้าง: ไฟล์ index.js แสดง Version & DateTime

Path: [skeleton]\node-api\src\routes\api\index.js

Language: Bash

const router = require('express').Router()

// GET: /api/v1
router.get('/', async function (req, res, next) {
  res.json({ 
    message: 'Last Updated: 2024-08-16 19:53 Version 0.0.1',
    datetime: new Date(),
  })
});

// Path: /api/v1/example
router.use('/example', require('./example.api'))

module.exports = router

ไฟล์: routes/api/index.js

[skeleton]/src/routes/api/index.js

สร้าง: ไฟล์ example.api.js ใช้เป็น RESTful API "ตัวอย่าง"

Path: [skeleton]\node-api\src\routes\api\example.api.js

Language: Bash

const router = require('express').Router()

// GET: /api/v1/example
router.get('/', async function (req, res, next) {
  res.status(200).json({ 
    code: 200, 
    message: 'OK',
  })
})

// GET: /api/v1/example/:id
router.get('/:id', async function (req, res, next) {
  res.status(200).json({
    code: 200,
    message: 'OK',
    data: req.params,
  })
})

// POST: /api/v1/example
router.post('/', async function (req, res, next) {
  res.status(201).json({
    code: 201,
    message: 'Created',
    data: req.body,
  })
})

// PUT: /api/v1/example/:id
router.put('/:id', async function (req, res, next) {
  res.status(200).json({
    code: 200,
    message: 'Updated',
    data: req.body,
  })
})

// DELETE: /api/v1/example/:id
router.delete('/:id', async function (req, res, next) {
  res.status(200).json({
    code: 204,
    message: 'Deleted',
    data: req.params,
  })
})

module.exports = router

ไฟล์: routes/api/example.api.js

[skeleton]/src/routes/api/example.api.jss

รัน Web Service ด้วย Express.js

Path: [skeleton]\node-api

Language: Project Structure

# รันโหมด Dev บน Local
$ npm run start:dev

## Output ##
# > node-api@0.0.1 start:dev
# > NODE_ENV=development && nodemon --exec babel-node src/index.js

# [nodemon] 3.1.4
# [nodemon] to restart at any time, enter `rs`
# [nodemon] watching path(s): *.*
# [nodemon] watching extensions: js,mjs,json
# [nodemon] starting `babel-node src/index.js`
# No env file present for the current environment:
#   Falling back to .env config
# Server is running at :::3000

ตัวอย่าง: RESTful API เส้นแสดง Detail ทั้งหมด

http://127.0.0.1:3000/api/v1

GET: ดึงข้อมูล (ทั้งหมด)

http://127.0.0.1:3000/api/v1/example

GET: ดึงข้อมูล

http://127.0.0.1:3000/api/v1/examle/1

CREATE: เพิ่มข้อมูล

http://127.0.0.1:3000/api/v1/examle

UPDATE: อัพเดทข้อมูล

http://127.0.0.1:3000/api/v1/examle/1

DELETE: ลบข้อมูล

http://127.0.0.1:3000/api/v1/examle/1