An experimental mash-up of RxJS and Express.
npm i rxxpress
The core of RxXpress is the Router
class, which behaves like
Express's Router
, except that instead of accepting a callback,
it returns a Subject
:
// router.ts; ;router.get'/'.piperespond'Hellow World!'.subscribe;
// index.ts;; ;app.userouter.core;app.listen3000;
👉Read the documentation for more information.
WHY?
Well I have ABSOLUTELY NO IDEA where this is going to be really useful. My intention was to be able to do weird stuff. For example, you can use it to do rate limiting on a particular endpoint:
;; ; router.get'/endpoint' .pipe debounceTime1000, // --> only respond to one request each second respond'Halo!' .subscribe;
Or you can do rate limiting per end-point per user:
;; ; router.get'/endpoint' .pipe useauthenticate, // --> some authentication method, populates `user_id` groupByreq.user_id, // --> group incoming requests by `user_id` mergeMapgroup.pipedebounceTime1000, // --> respond once per second per group respond'Halo!' .subscribe;
You can even do weirder stuff like responding to an endpoint only if two users with different keys request it at the same time:
;;; ;; zip endpoint.pipefilterreq.query.key === ALICE_KEY, endpoint.pipefilterreq.query.key === BOB_KEY,.pipe tap, retry.subscribe;
Interoperability
You can use RxXpress routers inside Express routers (check the first example).
RxXpress also provides the use()
pipeable operator, which provides seamless interoperability
with Express:
-
You can use it to pipe Express routers to RxXpress routers.
-
You can use it to pipe Express middlewares to RxXpress routers.
-
You can use it to pipe any request handler function
(req, res, next) => ...
to RxXpress routers. -
You can use it to pipe RxXpress routers together.
// sub-router.ts; ;router.get'/world'.subscriberes.send'Halo Welt!';router.get'/dude'.subscriberes.send'Hello My Dude!';router.get'/:name'.subscriberes.send`Hi `; ;
// router.ts;; ;router.use'/hello' .pipeusesubRouter .subscribe; ;
Now checkout /hello/world
, /hello/dude
and /hello/<whatever>
routers.
► TRY IT!
// ... ;; // ... ;xpRouter.get'/X',...; // ... router.use'/' .pipe usexpRouter, usebodyparser, use..., .subscribe;