Customization
This section will brief about how to add custom APIs (or update existing APIs) in your existing mockbee template.
Design an API#
Decide
- End Point (the url that you call from frontend)
- Methods (get, post etc.)
- Does it require Authentication?
- What the API returns
- What is the logic you want to put
Think in terms of Input -> Processing -> Output
View other implementations of the APIs too in the project
All the
logicis present insidebackend/controllers,datais inbackend/dband utility functions inbackend/utils.The API endpoints are present in
server.jsYou can update or add new based on this
Inside Backend Folder...#
In the
/src/backend, there are three folders:- controllers
- db
- utils
First, you can add a Handler function in corresponding
controllersfile. If you are adding new API or feature, you can create a new file asNewFeatureController.js.Add your logic inside that function. (Have a look at the code of other handlers to understand how you can also implement the same)
If you need some pre-seeding data for your feature, add inside
db.Any reusable utilities can be put inside
utils.
Add API Endpoint inside server.js#
- If you have any new models added, add it in models object. (Have a look at other added models)
- Add seeding data from db if anything new entry is in pre-seed.
- Inside routes, the routes are added as: `this.methodName(endpoint, callback)
- The callbacks are bind with
thisbecause we want to usethisreference in our handler function for db, authUtils etc.
Checking its working#
- Test the APIs with
mockman-js. - Integrate in App after the api endpoints are working well.
Further Steps#
- We are using
mirage-jsfor mock-backend. You can read these imp guides which are there on their official documentation: