API.js 793 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const express = require('express');
  2. const Logger = require('./Logger');
  3. const path = require('path');
  4. class API {
  5. constructor() {
  6. this.router = express.Router();
  7. this.namespace = '';
  8. this.path = '';
  9. this.method = 'get';
  10. this.logger = new Logger(path.join(__dirname, '../logs/API.log'), 'INFO');
  11. }
  12. setPath(path) {
  13. this.path = path;
  14. }
  15. setMethod(method) {
  16. this.method = method.toLowerCase();
  17. }
  18. getRouter() {
  19. return this.router;
  20. }
  21. async onRequest(req, res) {
  22. throw new Error('onRequest方法未实现');
  23. }
  24. setupRoute() {
  25. this.router[this.method](this.path, async (req, res) => {
  26. await this.onRequest(req, res);
  27. });
  28. }
  29. }
  30. module.exports = API;