MVC
This page is a guide to use the MVC pattern with Byte.
Controllers
Every Byte
instance is equivalent to a controller.
// Don't do thisapp.get('/', Controller.index);
// Do this insteadapp.get('/', (ctx) => { // Write the handler code here // Does not require manual typings});
Services
Some piece of code that can be decoupled from the controller should be separated into a service:
export const getNumbers = query.get('number', { type: 'number', maxItems: 5000});
import * as service from './service';
app.get('/numbers', (ctx) => ctx.body( service.getNumbers(ctx).join()));
Models
Models can be handled using states in Byte:
export const validateBody = Byte.handle((ctx) => { // Return a result or a Response object});
import * as model from './model';
app.state('body', model.validateBody) .post('/json', (ctx) => { // Use ctx.body here });