send-response

1.0.0 • Public • Published

send-response

Send HTTP responses.

npm travis standard conduct

Install

npm install --save send-response

Usage

var intoStream = require('into-stream')
 
var json = require('send-response/json')
var html = require('send-response/html')
var text = require('send-response/text')
var error = require('send-response/error')
var redirect = require('send-response/redirect')
 
// send a text response
function textResponse (req, res) {
  text(res, 'ok')
}
 
// stream text to the response
function textStreamResponse (req, res) {
  var stream = intoStream('hi')
  text(res, 200, stream)
}
 
// respond with json
function jsonResponse (req, res) {
  json(res, 201, { message: 'hi' })
}
 
// stream json to the response
function jsonStreamResponse (req, res) {
  var stream = intoStream.obj({ wut:'wut' })
  json(res, stream)
}
 
// respond with html
function htmlResponse (req, res) {
  html(res, 201, '<h1>hi</h1>')
}
 
// stream html to the response
function htmlStreamResponse (req, res) {
  var stream = intoStream('<h1>hi</h1>')
  html(res, stream)
}
 
// redirect a request to another url
function redirectResponse (req, res) {
  redirect(res, 302, 'http://127.0.0.1:3000/redirected')
}
 
// example of redirected handler
function redirected (req, res) {
  send(res, 'redirected!')
}
 
// send an error response with JSON: { statusCode: 404, message: 'Not Found' }
function errorJsonResponse (req, res) {
  error(res, 'Not Found')
}

Examples

  • appa-api examples of using send-response with appa-api
  • http examples using node's http module

Contributing

Contributions are welcome! Please read the contributing guidelines first.

Conduct

It's important that this project contributes to a friendly, safe, and welcoming environment for all, particularly for folks that are historically underrepresented in technology. Read this project's code of conduct

Change log

Read about the changes to this project in CHANGELOG.md. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Contact

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i send-response

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • sethvincent