Skip to content

Instantly share code, notes, and snippets.

@dotob
Created September 16, 2016 13:48
Show Gist options
  • Save dotob/8d58fbed42a6a0850e2bbc91f15c0ed2 to your computer and use it in GitHub Desktop.
Save dotob/8d58fbed42a6a0850e2bbc91f15c0ed2 to your computer and use it in GitHub Desktop.

Revisions

  1. dotob created this gist Sep 16, 2016.
    37 changes: 37 additions & 0 deletions index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import * as express from 'express';
    var app = express();

    app.get('/', function (req, res) {
    res.send('Hello World!');
    });

    app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
    });

    export interface IQueryResult {
    data: string;
    }

    function RouteDecorator(app: express.Application, route: string) {
    return function (
    target: any,
    propertyKey: string,
    descriptor: TypedPropertyDescriptor<() =>Promise<IQueryResult>>) {
    console.log(`create route ${route} for method ${propertyKey}`);
    app.get(route, async (req, res) => {
    let r = await target[propertyKey]();
    res.send(r);
    });
    };
    }

    export class Queries {
    @RouteDecorator(app, "/a")
    bla(): Promise<IQueryResult> {
    return new Promise<IQueryResult>((resolve, reject) => {
    console.log("bla");
    resolve({ data: 'blablablablabla' });
    })
    }
    }