Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alessioalex/inject-html

Node module for injecting HTML code before or after the `body` tag into the response.
https://github.com/alessioalex/inject-html

html inject inject-script nodejs

Last synced: 22 days ago
JSON representation

Node module for injecting HTML code before or after the `body` tag into the response.

Awesome Lists containing this project

README

        

# inject-html

Node module for injecting HTML code before or after the `body` tag into the response.

[![Build Status](https://travis-ci.org/alessioalex/inject-html.svg)](https://travis-ci.org/alessioalex/inject-html)

## usage

Installable via npm: `npm i inject-html`.

```js
var inject = injectCode({
// type: 'append', // default 'prepend'
code: "alert('injected!!')" // HTML code
});

http.createServer(function(req, res) {
inject(req, res, function() {
res.setHeader('Content-Type', 'text/html');

if (req.url === '/') {
fs.createReadStream(__dirname + '/page.html').pipe(res);
} else {
res.statusCode = 404;
res.end('Page Not Found\n');
}
});
}).listen(1337);
```

Sample output:

before injecting:

```html


inject html example

inject-html example


content

```

after injecting:

```html


inject html example

alert('injected!!')

inject-html example


content

```

## caveats

If the content doesn't have a `` tag, this module __will not function properly__.
This module will delete the `content-length` and `content-encoding` (if gzip/inflate) headers.

## license

[MIT](http://alessioalex.mit-license.org/)