use()
respond()
check()
wait()
join()
reject()
next()
timeout()
use()
operatoruse()
operator lets you pipe various types of request handlers to your
RxXpress routers:
1import { Router, use, respond } from 'rxxpress'; // --> import the operator
2
3const router = new Router();
4router.get('/:name').pipe(
5 use((req, res, next) => { // --> lets use a classic request handler
6 if (req.params.name === 'dude') // --> is it the dude?
7 res.send('You are the dude!'); // --> its the dude!!
8 else next(); // --> nope? invoke the next handler
9 }),
10 respond(() => 'I do not know you!') // --> this guy is the next handler
11).subscribe();
This means you can easily use any Express middleware with your RxXpress routers:
1import cookieParser from 'cookie-parser';
2import bodyParser from 'body-parser';
3import { Router, use } from 'rxxpress';
4
5const router = new Router();
6router.get('/').pipe(
7 use(cookieParser),
8 use(bodyParser),
9 ...
10).subscribe();
You can also utilize the use()
operator to mount sub-routers on different paths:
1import { Router, use } from 'rxxpress';
2import subRouter from './sub-router';
3import authenticate from './auth';
4
5const router = new Router();
6router.use('/sub')
7.pipe(
8 authenticate(),
9 use(subRouter), // --> mount the sub-router
10)
11.subscribe();
1import { Router, respond } from 'rxxpress';
2
3const router = new Router();
4router.get('/hellow').pipe(respond(() => 'Hellow!')).subscribe();
5
6export default router;
You can similarly mount Express routers on different paths:
1import { Router, use } from 'rxxpress';
2import subRouter from './sub-router';
3import authenticate from './auth';
4
5const router = new Router();
6router.use('/sub')
7.pipe(
8 authenticate(),
9 use(subRouter), // --> mount the sub-router
10)
11.subscribe();
1import { Router } from 'express'; // --> creating an express sub-router
2
3const router = Router();
4router.get('/hellow', (_, res) => res.send('Hellow!'));
5
6export default router;
The use()
operator will not pass incoming packets down the observable sequence:
1router.get('/')
2.pipe(
3 use((req, res, next) => {
4 // do nothing
5 }),
6 tap(() => console.log('Got Here!')) // --> you will NEVER get here
7)
8.subscribe();
In this example, you will never get the log 'Got Here!'
, unless the preceding
request handler invokes next()
:
1router.get('/')
2.pipe(
3 use((req, res, next) => {
4 next();
5 }),
6 tap(() => console.log('Got Here!')) // --> you will ALWAYS get here
7)
8.subscribe();