then-busboy
Promise-based wrapper around Busboy, inspired by async-busboy
Installation
Note: The current documentation is for 2.x versions of then-busboy.
If you're looking for a previous version, check out the 1.x branch.
Use can install then-busboy
from npm:
npm install --save then-busboy
Or with yarn:
yarn add then-busboy
API
busboy(request[, options]) -> Promise<Object>
- http.IncomingMessage request – HTTP request object
- object options
- boolean restoreTypes – allow to restore type of each value (default – true)
- more information about busboy options here.
constructor File(options)
- object options – an object that contains the following information about file:
- stream.Readable contents – the content of the file.
- string filename – name of the file (with an extension)
- string env – encoding of the file content
- string mime – file mime type
Instance properties
contents
A file contents Readable stream.
stream
An alias for contents
filename
A full name of the file
basename
A name of the file without extension
extname
A file extension
mime
A file mime type
enc
File contents encoding
path
Default path of the file
Instance methods
read() => Promise<Buffer>
Read a file from contents stream.
write([path]) => Promise<void>
Write a file content to disk. Optionally you can set a custom path.
By default, file will be saved in system temporary directory os.tmpdir()
.
You can take this path from path property.
Usage
Just import then-busboy
and pass request
object as first argument.
import busboy from "then-busboy"
import {createServer} from "http"
function handler(req, res) {
function onFulfilled(data) {
res.writeHead("Content-Type", "application/json")
res.end(JSON.stringify(data))
}
function onRejected(err) {
res.statusCode = err.status || 500
res.end(String(err))
}
busboy(req).then(onFulfilled, onRejected)
}
createServer(handler)
.listen(2319, () => console.log("Server started on http://localhost:2319"))
then-busboy
always returns a Promise, so you can use it with
asynchronous function syntax:
const data = await busboy(req)
License
MIT