Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/inject-body
Inject contents into the HTML <body> tag of an HTTP response
https://github.com/shinnn/inject-body
html http javascript modification nodejs non-blocking response stream xml
Last synced: 27 days ago
JSON representation
Inject contents into the HTML <body> tag of an HTTP response
- Host: GitHub
- URL: https://github.com/shinnn/inject-body
- Owner: shinnn
- License: isc
- Created: 2018-08-06T06:17:38.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-13T07:25:13.000Z (about 6 years ago)
- Last Synced: 2024-10-14T15:02:30.631Z (about 1 month ago)
- Topics: html, http, javascript, modification, nodejs, non-blocking, response, stream, xml
- Language: JavaScript
- Size: 35.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# inject-body
[![npm version](https://img.shields.io/npm/v/inject-body.svg)](https://www.npmjs.com/package/inject-body)
[![Build Status](https://travis-ci.com/shinnn/inject-body.svg?branch=master)](https://travis-ci.com/shinnn/inject-body)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/inject-body.svg)](https://coveralls.io/github/shinnn/inject-body?branch=master)Inject contents into the HTML `` tag of an HTTP response
```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const injectBody = require('inject-body');createServer((req, res) => {
injectBody(res, Buffer.from('Hello '));res.setHeader('content-type', 'text/html');
res.end('World');
}).listen(3000, async () => {
return (await fetch('http://localhost:3000')).text(); //=> 'Hello, World'
});
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
```
npm install inject-body
```## API
```javascript
const injectBody = require('inject-body');
```### injectBody(*response*, *contents*)
*response*: [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse)
*contents*: `Buffer`If the media type of the response is `text/html`, it inserts a given contents into the response body as the first child of `` tag, with increasing the value of `Content-Length` header if necessary.
```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const injectBody = require('inject-body');const html = Buffer.from('
Hi
');
const inserted = Buffer.from('🏄
');createServer((req, res) => {
injectBody(res, inserted);res.setHeader('content-type', 'text/html');
res.setHeader('content-length', 37/* Buffer.byteLength(html) */);
res.end(html);
}).listen(3000, async () => {
const response = await fetch('http://localhost:3000');Number(response.headers.get('content-length'));
//=> 53, Buffer.byteLength(html) + Buffer.byteLength(inserted)return response.text(); //=> '
🏄
Hi
'
});
```If the media type is not `text/html` or the response body has no `` tag, it does nothing.
### class injectBody.InjectBody(*contents*)
*contents*: `Buffer`
Return: `Function`Create a new `injectBody` function with the fixed `contents` argument. Use this class if a server will inject the same contents into every HTML response many times.
```javascript
const {InjectBody} = require('inject-body');const injectStyle = new InjectBody(Buffer.from('body {color: red}'));
```## License
[ISC License](./LICENSE) © 2018 Shinnosuke Watanabe