send-response
v1.0.0
Published
send HTTP responses
Downloads
7
Readme
send-response
Send HTTP responses.
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
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
- issues – Please open issues in the issues queue