Star

Created With

linkrespond() and json()

respond() operator allows for easily responding to requests with OK http status (200) and some text message:

1import { respond } from 'rxxpress';

2

3router.get('/')

4.pipe(respond(() => 'Hellow World!'))

5.subscribe();


You have access to the packet inside the respond() operator:

1router.get('/:name')

2.pipe(respond(({req}) => `Hellow ${req.params.name}`))

3.subscribe();


If you want to respond with a JSON, simply use json() operator instead. It is identical to respond() except that it responds with JSON instead of text:

1import { json } from 'rxxpress';

2

3router.get('/:name')

4.pipe(

5 json(({req}) => ({ message: `Hellow ${req.params.name}` }))

6)

7.subscribe();


linkAsync Functions

You can use async functions inside respond() and json():

1router.get('/:name')

2.pipe(

3 respond(async ({req}) => {

4 const user = await db.find({ name: req.params.name });

5 return `Hellow ${user.full_name}`;

6 })

7)

8.subscribe();

1router.get('/:name')

2.pipe(

3 json(async ({req}) => {

4 const user = await db.find({ name: req.params.name });

5 return {

6 message: `Hellow ${user.full_name}`,

7 recipient: user

8 };

9 })

10)

11.subscribe();


linkObservable Functions

You can also return observables inside respond() and json():

1router.get('/')

2.pipe(

3 respond(() => of('Hellow World!').pipe(delay(1000)))

4)

5.subscribe();

1router.get('/')

2.pipe(

3 json(() => of({ msg: 'Hellow World!' }).pipe(delay(1000)))

4)

5.subscribe();

When you provide an observable, the first value emitted by that observable will be used as response and the observable will be unsubscribed from.


linkSafety

respond() and json() only respond IF incoming request was not already responded to:

1router.get('/')

2.pipe(

3 respond(() => 'AA'),

4 respond(() => 'BB'),

5)

6.subscribe();

In the case of this example, the response will always be 'AA'.


linkPacket Flow

respond() and json() never pass incoming packets down the observable stream, nor they invoke any other request handlers:

1router.get('/')

2.pipe(

3 respond(() => 42),

4 tap(() => console.log('Got Here!')) // --> YOU WILL NEVER GET HERE!

5).subscribe();

So for example in the above case, you will never get the console log.

respond() and json()Async FunctionsObservable FunctionsSafetyPacket Flow

Home Router

Operatorschevron_right

Error Handling