Skip to content

srcagency/http-tell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-tell

A simple layer on top of response.writeHead/write that supports passing data from buffers, strings or pull-streams, encoding it as ndjson or as is.

tell(response, {
	// headers
	code,		// defaults to 200
	message,
	headers,	// { 'Content-Type': 'image/jpeg' }

	// body
	json,		// pull-stream or string
	raw,		// pull-stream or whatever
}, cb)

Examples

var tell = require('http-tell');

http.createServer(function( request, response ){
	tell(response, {
		201,

		json: { its: 'okay' },
	}, err => err
		? console.error(err)
		: console.log('Completed a request'));
})
	.listen(8000);

Streaming JSON:

var pull = require('pull-stream');
var tell = require('http-tell');

http.createServer(function( request, response ){
	tell(response, {
		json: pull(
			pull.values([ 'a', 'b', 'c' ]),
			pull.map(i => i.toUpperCase())
		),
	}, err => err
		? console.error(err)
		: console.log('School\'s over'));
})
	.listen(8000);

Streaming a file:

var pull = require('pull-stream');
var file = require('pull-file');
var tell = require('http-tell');

http.createServer(function( request, response ){
	tell(response, {
		headers: {
			'Content-Type': 'image/jpeg',
		},
		raw: file(__dirname+'/kitten.jpg'),
	}, err => err
		? console.error(err)
		: console.log('Sent a kitten'));
})
	.listen(8000);

About

Send off stuff to a http response

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published