rcmedeiros / template
1
// tslint:disable: no-unsafe-any no-implicit-dependencies
2 2
import bodyParser from 'body-parser';
3 2
import e, { NextFunction, Request, Response } from 'express';
4
import * as core from 'express-serve-static-core';
5 2
import figlet from 'figlet';
6 2
import fs from 'fs';
7
import * as http from 'http';
8 2
import path from 'path';
9

10
interface ModuleInfo {
11
    name: string;
12
    version: string;
13
    description: string;
14
}
15

16
interface Closable {
17
    close(callback?: Function): this;
18
}
19

20 2
export class DummyServer {
21 2
    private readonly LIST: Array<string> = ['Green Tea', 'Himalaya Darjeeling', 'Earl Grey tea'];
22
    private readonly app: core.Express;
23
    private readonly moduleInfo: ModuleInfo;
24
    private server: http.Server;
25

26 2
    constructor() {
27 2
        this.app = e();
28

29 2
        this.app.use(bodyParser.json());
30 2
        this.app.use(bodyParser.urlencoded({ extended: false }));
31 2
        this.app.use(bodyParser.raw());
32

33 2
        this.app.use('/coffee', (request: Request, response: Response) => {
34 2
            response.sendStatus(418);
35
        });
36 2
        this.app.use('/tea', (request: Request, response: Response) => {
37 2
            const t: string = request.query && request.query.t ? request.query.t : request.body && request.body.t ? request.body.t : undefined;
38 2
            if (!(t)) {
39 2
                response.send('Which one?');
40 2
            } else if (!this.LIST.includes(t)) {
41 2
                response.send(`${t} unavailable. Try another.`);
42
            } else {
43 2
                response.json({ tea: t });
44
            }
45
        });
46

47
        // This route matches even unknown routes that starts with /. Thus the conditional
48 2
        this.app.use('/', (request: Request, response: Response, next: NextFunction) => {
49 2
            if (request.path === '/') {
50 2
                response.json(this.LIST);
51
            } else {
52 2
                next();
53
            }
54
        });
55

56 2
        this.moduleInfo = this.getModuleInfo();
57

58
    }
59

60 2
    private getModuleInfo(): ModuleInfo {
61 2
        let filename: string = 'package.json';
62

63 2
        while (!fs.existsSync(path.join(__dirname, filename))) {
64 2
            filename = '../' + filename;
65
        }
66

67 2
        const packageJson: Buffer = fs.readFileSync(path.join(__dirname, filename));
68 2
        const project: unknown = JSON.parse(packageJson.toString('utf-8'));
69

70 2
        return {
71
            name: (project as ModuleInfo).name,
72
            version: (project as ModuleInfo).version,
73
            description: (project as ModuleInfo).description,
74
        };
75
    }
76

77 2
    public async listen(): Promise<DummyServer> {
78 2
        return new Promise((resolve: Function, reject: Function): void => {
79 2
            this.server = this.app.listen(8080, () => {
80 2
                figlet.text('Dummy Online', {
81
                    font: 'Star Wars',
82
                    horizontalLayout: 'default',
83
                    verticalLayout: 'default',
84 2
                }, (err: Error, data: string) => {
85 2
                    if (err) {/* istanbul ignore next */
86
                        reject(err);
87
                    } else {
88 2
                        console.info(`${data}\nv${this.moduleInfo.version}\n`);
89 2
                        resolve(this);
90
                    }
91
                });
92
            });
93
        });
94
    }
95

96 2
    public async close(): Promise<void> {
97 2
        return new Promise((resolve: Function, reject: Function): void => {
98 2
            this.server.close((err: Error) => {
99 2
                if (err) {/* istanbul ignore next */
100
                    reject(err);
101
                } else {
102 2
                    resolve();
103
                }
104
            });
105
        });
106
    }
107
}

Read our documentation on viewing source code .

Loading