https://github.com/shinnn/insert-html-content
Insert contents into an HTML of a response body
https://github.com/shinnn/insert-html-content
html html5 insertion javascript modification nodejs non-blocking response stream
Last synced: 2 months ago
JSON representation
Insert contents into an HTML of a response body
- Host: GitHub
- URL: https://github.com/shinnn/insert-html-content
- Owner: shinnn
- License: isc
- Created: 2018-10-31T06:37:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-28T13:27:07.000Z (almost 6 years ago)
- Last Synced: 2024-10-18T19:56:33.302Z (8 months ago)
- Topics: html, html5, insertion, javascript, modification, nodejs, non-blocking, response, stream
- Language: JavaScript
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# insert-html-content
[](https://www.npmjs.com/package/insert-html-content)
[](https://wdp9fww0r9.execute-api.us-west-2.amazonaws.com/production/results/shinnn/insert-html-content)
[](https://codecov.io/gh/shinnn/insert-html-content)Insert contents into an HTML of a response body
```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const insertHtmlContent = require('insert-html-content');createServer((req, res) => {
insertHtmlContent(res, 'Hello ');res.setHeader('content-type', 'text/html');
res.end('World');
}).listen(3000, async () => {
await (await fetch('http://localhost:3000')).text(); //=> 'Hello, World'
});
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install insert-html-content
```## API
```javascript
const insertHtmlContent = require('insert-html-content');
```### insertHtmlContent(*response*, *content* [, *options*])
*response*: [`http.ServerResponse`](https://nodejs.org/api/http.html#http_class_http_serverresponse)
*content*: `string`
*options*: `Object`If the media type of the response is `text/html`, it inserts a given content into the response body as the first child of `` tag once, with increasing the value of `content-length` header if necessary.
```javascript
const {createServer} = require('http');
const fetch = require('node-fetch');
const injectBody = require('insert-html-content');const html = Buffer.from('
Hi
');
const inserted = '🏄
';createServer((req, res) => {
insertHtmlContent(res, inserted);res.setHeader('content-type', 'text/html');
res.setHeader('content-length', 37/* html.length */);
res.end(html);
}).listen(3000, async () => {
const response = await fetch('http://localhost:3000');Number(response.headers.get('content-length'));
//=> 53, html.length + Buffer.byteLength(inserted)await response.text(); //=> '
🏄
Hi
'
});
```If the media type is not `text/html`, or the response body has no `` tag, it does nothing.
### options.tagName
Type: `string`
Default: `'body'`Change the insertion target to the given tag.
```javascript
createServer((req, res) => {
insertHtmlContent(res, '', {
tagName: 'head'
});res.setHeader('content-type', 'text/html');
res.end('');
}).listen(3000, async () => {
await (await fetch('http://localhost:3000')).text(); //=> ''
});
```### options.insertToEnd
Type: `boolean`
Default: `false`When this option is `true`, it inserts a content to the last child of the target tag instead.
Default:
```html
existing contentinserted content
````insertToEnd: true`:
```html
inserted contentexisting content
```### class insertHtmlContent.InsertHtmlContent(*contents* [, *options*])
*content*: `string`
*options*: `Object`
Return: `Function`Create a new `insertHtmlContent` function with the fixed `content` and `options`. Use this class if a server will insert the same contents into every HTML response many times.
```javascript
const {InsertHtmlContent} = require('insert-html-content');const injectStyle = new InsertHtmlContent('body {color: red}');
```## License
[ISC License](./LICENSE) © 2018 - 2019 Watanabe Shinnosuke