零依赖性HTTP响应功能

零依赖性HTTP响应功能

Node.js HTTP工具

访问GitHub主页

共36Star

详细介绍

http-responders

Zero-dependency HTTP response functions.

Supports:

  • json
  • redirect
  • stream
  • file
  • download

Example

const { json, redirect, stream, file, download } = require('http-responders')

http.createServer((req, res) => {

  // Respond with JSON
  json(res, { beep: 'boop' })

  // Redirect
  redirect(req, res, 'https://example.com/')

  // Respond with a generic stream
  stream(res, fs.createReadStream('file.txt'))

  // Respond with a file (+ content length)
  file(res, 'file.txt')

  // Make the browser download the file
  download(res, 'file.txt)

})

Installation

$ npm install http-responders

API

.json(res, json)

.redirect(req, res, location)

await .stream(res, stream)

await .file(res, path, [fsOpts])

await .download(res, path, [fsOpts])

Sponsors

Development of this module is sponsored by:

Liberate Science

License

MIT