Created
September 16, 2016 13:48
-
-
Save dotob/8d58fbed42a6a0850e2bbc91f15c0ed2 to your computer and use it in GitHub Desktop.
express route per typescript decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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