2.6 Routes - Baumgaer/Game GitHub Wiki
A route is just like a model a BDO. So the most part of the logic should be contained in the BDO section.
/**
* constructs the base for the route on server and client side.
*/
export function BDOMyRouteFactory<TBase extends Constructor<BDORoute>>(ctor: TBase) {
/**
* Provides basic functionality for the "home page" of the website
*
* @export
* @class GameLobby
* @extends {BaseRoute}
*/
abstract class BDOMyRoute extends ctor {
/**
* Defines which servers have to use this route.
* "*" (default) means that all servers should use this route.
* If only a specific number of server should use this route, define their
* names in the array.
*/
public static attachToServers: string[] = ["WebServer"];
/**
* this defines where to reach the route. In this case it is
* www.example.com and by default it is www.example.com/<nameOfTheRoute>.
*/
public routerNameSpace: string = '/';
/**
* This defines several routes which can be used after the
* routerNameSpace. At the moment it uses
* www.example.com/<routerNameSpace>/site/<aNumber>.
* You can define more routes and must handle them in the
* templateParams() method.
*/
public routes: string[] = ['/site/:number'];
/**
* This has the same functionality like the templateString in a
* component. In case a user comes the first time to this project. the
* server will render the first page. After that the server should
* receive a header named "X-Game-As-JSON" which forces the server to
* send json only.
*/
protected templateString: Template = require('~bdo/views/route.njk');
/**
* is called on HTTP GET and should return a dictionary with template
* parameters.
*/
protected async templateParams(_request: Request): Promise<IndexStructure> {
return {
someParam: 'some value'
};
}
}
return MyRoute;
}
At the moment the behavior for POST, DELETE, PATCH and PUT are not implemented but the areas where to define the behavior are already available.
To get the route collected on the server and client side you have to define the following on server and client side:
import {BDOMyRouteFactory} from "~bdo/routes/BDOMyRoute";
import {ServerRoute} from "~server/lib/ServerRoute";
/**
* Defines a route on server side. Please NOTE the default export!
*/
export default class GameLobby extends BDOMyRouteFactory(ServerRoute) {}