Home Ask Login Register

Developers Planet

Your answer is one click away!

sreginogemoh February 2016

Is there an analogue of mvc action filters in typescript with node

In aspnet mvc we can do such concept called action filters

[Authorize]
public ActionResult GetUsers()
{}

So when GetUsers() gets hit the Authorize action occurs first and based on its logic GetUsers() even wont get executed if user is not authorized.

I am writing my node server code with typescript and I know there is such concept in typescript called decorators which is used in Angular2 but I am not sure if it is applies to node too.

Currently In node I have my handlers/filters declared in separate .ts file And here is how I using them in my app.ts

import * as RequestHandlers from './infrastructure/handlers/requesthandlers';

app.use('/users', RequestHandlers.Authorize, users);

It is sort of OK but I just wondering if it is possible to do something like:

@whatewer { RequestHandlers.Authorize };
app.use('/users', users);

So RequestHandlers.Authorize gets executed before app.use('/users', users);

May be some one have any ideas?

Answers


basarat February 2016

It is sort of OK but I just wondering if it is possible to do something like

No. The decorator syntax is just a sugar over a function wrapper can only be used on variables,functions,classes etc. You cannot decorate an arbitrary statement.

Post Status

Asked in February 2016
Viewed 3,673 times
Voted 12
Answered 1 times

Search




Leave an answer


Quote of the day: live life