How to use the routing - AvengerDisassemble/KU-connect GitHub Wiki

How it works

The routing in KU-connect follows a file structure. The file hierarchy represents the path of the endpoints. For example:

/routes
│
├── /example                      
│   ├── /example2.js
│   ├── /folder           
│       ├── /example3.js 
|       ├── /index.js                
├── /example4.js      
├── /index.js    

The API endpoints will be automatically set as:

  1. /api/example/example2
  2. /api/example/folder/example3.js
  3. /api/folder/
  4. /api/example4
  5. /api/ (All routes must be prefixed by /api/)

Notes

  • A file called index.js will be the default file. The path name will be registered as if the name index were not there. For example, someFolder/index.js will be interpret as /someFolder/ not /someFolder/index/
  • The routes/index.js or the outermost index.js file contains the whole routing logic in it.

How to use it

  1. Create a file inside the desired hierarchy.
  2. Adding the code similar to this example:
/**
 * Example route for demonstration purposes
 * @module routes/example
 */

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

// Why: Demonstrates a simple GET endpoint for testing route setup
router.get('/', (req, res) => {
  res.json({ message: 'This is an example route!' })
})

module.exports = router

The module must use a different router from the one in the outermost index.js and export the router. 3. That's it! Just live it there, and the code will organize itself.

Conventions

  1. If there's a folder named something, a file .js of that name should not exist.
DON'T DO THIS
/routes
│
├── /example                      
│   ├── /example2.js
├── /example.js       # Duplicate name with the folder
DO THIS INSTEAD
/routes
│
├── /example                      
│   ├── /example2.js
│   ├── /index.js      # Change to index.js