import { Router} from 'express';
import MyController from '../Controllers/MyController'
const router = Router();
router.get('/api-route', MyController.getData);
export default router;
我的控制器.ts:
import { Request, Response } from 'express';
import MyController from '../Controllers/MyController'
export default class MyController {
public static async getData(request: Request, response: Response) {
const body: any = request.body;
// Do request validation... (e.g. Validate JWT)
let data: Data | undefined = await MyService.getData(body["secret-code"]);
if (!data)
return response.status(403).json("Wrong secret code!");
return response.status(200).json(data);
}
}
我的服务.ts
export default class MyService {
public static async getData(secretCode: string): Data | undefined {
if (secretCode !== "TheSecret")
return undefined;
return await MyRepository.getData();
}
}
我的存储库.ts
export default class MyRepository {
public static async getData(): Data {
// This is pseudo code to get data from database.
return await Database.Connect(EnvVariableConnectionString).Get("data");
}
}