Skip to content

Instantly share code, notes, and snippets.

@dotob
Created September 16, 2016 13:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotob/8d58fbed42a6a0850e2bbc91f15c0ed2 to your computer and use it in GitHub Desktop.
Save dotob/8d58fbed42a6a0850e2bbc91f15c0ed2 to your computer and use it in GitHub Desktop.
express route per typescript decorator
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' });
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment