Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Koa middleware to return streaming JSON responses using AsyncIterators

License

NotificationsYou must be signed in to change notification settings

koajs/json-async-iterator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Koa JSON response body streaming for async iterators

Usage

constjsonAsyncIterator=require('@koa/json-async-iterator')constapp=newKoa()app.use(jsonAsyncIterator())app.use(asyncctx=>{constiterator=asyncfunction*(){yieldPromise.resolve({foo:'bar'})yieldPromise.resolve({baz:'qux'})}ctx.body=iterator()})

Output:

[{"foo":"bar"},{"baz":"qux"}]

Example with PostgreSQL:

importQueryStreamfrom'pg-query-stream'importpgfrom'pg'constclient=newpg.Client()awaitclient.connect()// later in your middlewareapp.use(async(ctx)=>{// stream all the rows from the database to the clientctx.body=client.query(newQueryStream(`        SELECT *        FROM posts        ORDER BY id ASC    `))})

Options

before

The string to write before the first value in the iterator.

Default:'[\n'

separator

The string to write between each value in the iterator.

Default:'\n,\n'

after

The string to write after the last value in the iterator.

Default:'\n]'

formatError

A function that takes an error and returns a value to be stringified.

Default:(err) => ({ error: { message: err.message } })

pretty

Whether to pretty-print the JSON.

Default:false

spaces

The number of spaces to use for pretty-printing.

Default:2

License

MIT

About

Koa middleware to return streaming JSON responses using AsyncIterators

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp