A response helper that detects & handles Content-Types
npm install @polka/send-type> An HTTP response helper that detects Content-Types & handles them accordingly — _not_ limited to [Polka][polka]!
For a simpler, bare-bones alternative, check out [@polka/send][send] instead~
```
$ npm install --save @polka/send-type
`js
const { createReadStream } = require('fs');
const send = require('@polka/send-type');
module.exports = function (req, res) {
if (!req.getHeader('authorization')) {
// Objects are converted to JSON
return send(res, 401, { error:'Token required!' });
}
// Streams & Buffers are auto-piped
// Your 'Content-Type' is always used,
// ~> otherwise 'application/octet-stream'
let file = createReadStream('massive.mp4');
send(res, 206, file, { 'Content-Type': 'video/mp4' });
}
`
#### res
Type: ServerReponse
The outgoing HTTP response.
#### code
Type: Number200
Default:
The statusCode for your response.
#### data
Type: String''
Default:
The body for your response. Defaults to the statusText for the given statusCode.
See Data Detections for special behaviors.
#### headers
Type: Object{}
Default:
The headers for your response.
The Content-Type header is a little unique – it will be set with the value you provide in headers. However, if you _did not_ set a value explicitly, then send-type will reuse the existing value via res.getHeader.Content-Type
If neither existed, then the will be inferred by the data type.
See Data Detections for special behaviors.
The following operations will be performed for the following data types:
> Important: If this is too much magic for you, check out [@polka/send][send] instead!
to 'application/octet-stream', unless one exists in headers
- Sets Content-Length$3
- Casts data to string via JSON.stringify
- Sets Content-Type to 'application/json; charset=utf-8', unless one exists in headers
- Sets Content-Length$3
- Sets Content-Type to 'application/octet-stream', unless one exists in headers
- Pipes data into the res directly
Support
Any issues or questions can be sent to the [Polka][polka] repo, but please specify that you are using
@polka/send-type`.MIT © Luke Edwards
[polka]: https://github.com/lukeed/polka
[send]: https://github.com/lukeed/polka/tree/master/packages/send