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:
- /api/example/example2
- /api/example/folder/example3.js
- /api/folder/
- /api/example4
- /api/ (All routes must be prefixed by /api/)
Notes
- A file called
index.jswill be the default file. The path name will be registered as if the name index were not there. For example,someFolder/index.jswill be interpret as/someFolder/not/someFolder/index/ - The
routes/index.jsor the outermostindex.jsfile contains the whole routing logic in it.
How to use it
- Create a file inside the desired hierarchy.
- 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
- 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