1
0
Fork 0

Add express server class

refactor/models
Matthew 2019-11-16 19:24:24 -05:00
parent ea1f20afc6
commit ed9d095e78
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
2 changed files with 48 additions and 0 deletions

46
src/api/Server.ts Normal file
View File

@ -0,0 +1,46 @@
/* eslint-disable no-useless-return */
import express from 'express';
import bodyParser from 'body-parser';
import fs from 'fs-extra';
import { Client } from '..';
import { Security } from '.';
import { Route } from '../class';
export default class Server {
public routes: Map<string, Route>;
public client: Client;
public security: Security;
public app: express.Express;
public options: { port: number }
constructor(client: Client, options?: { port: number }) {
this.options = options;
this.routes = new Map();
this.client = client;
this.security = new Security(this.client);
this.app = express();
this.connect();
this.loadRoutes();
}
private async loadRoutes(): Promise<void> {
const routes = await fs.readdir('./routes');
routes.forEach(async (routeFile) => {
if (routeFile === 'index.js') return;
const route: Route = new (require(`./${routeFile}`))(this);
this.routes.set(route.conf.path, route);
this.app.use(route.conf.path, route.router);
});
}
private connect(): void {
this.app.use(bodyParser.json());
this.app.listen(this.options.port, () => {
this.client.signale.success(`API Server listening on port ${this.options.port}`);
});
}
}

2
src/api/index.ts Normal file
View File

@ -0,0 +1,2 @@
export { default as Server } from './Server';
export { default as Security } from './Security';