express-script-injector

0.0.2 • Public • Published

This middleware was created to directly inject javascript files into <script> tags via templates rendered from the Express JS framework.

Installation

npm install express-script-injector --save

Usage

 
var injector = require('express-script-injector');
 
var options = {
    path: __dirname + '/scripts' // **required** path to your scripts folder (default: undefined)
    debug: false // *optional* Enable debugging to console  (default: false)
    script: 'index.js' //  *optional* Script to inject  (default: index.js)
    enableCache: false //  *optional* Cache previously loaded scripts to RAM  (default: false)
    useRoute: false //  *optional* If set to true, it will attempt to match the root route and load a script with the same name. (default: false)
}
 
app.use(injector(options));
 
// OR
 
router.use(injector(options));

The injector middleware will then expose the content of a js file inside req._script, so that you can render it with a template (see example below)

Example

This example shows how it might be used with an express application using handlebars templating.

 
var express  = require('express'),
    exphbs   = require('express-handlebars'),
    injector = require('express-script-injector');
 
var app = express();
 
// Apply the injector middleware (note: it could also just be added to a router instead)
app.use(injector({path: __dirname + '/scripts', script: 'main.js'}));
 
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
 
app.get('/', function (req, res) {
 
    // middleware exposes the content of the js file in req._script
    res.render('home', {injectedScript: req._script});
 
});
 
app.listen(3000);
 

Then somewhere in your home.hbs file:

{{{injectedScript}}}

Router Example

 
var
    router  = require('express').Router(),
    inject = require('express-script-injector');
 
router.use(inject({path: __dirname + '/../scripts'}));
 
router.get('*', function(req, res, next) {
    try {
        res.render('index', {script: req._script});
    } catch(e) {
        next(e);
    }
});
 
module.exports = router;

This outputs the js in a <script> tag with the id of "injector-" + the file name

Options

Name Description Default
path(required!) path to your scripts folder undefined
debug Enable debugging to console false
script file to inject index.js
enableCache Cache previously loaded scripts to RAM false
useRoute Experimental: If set to true, it will attempt to match the root route and load a script with the same name. If it does not find a file, it "falls back" load the script option. false

Package Sidebar

Install

npm i express-script-injector

Weekly Downloads

1

Version

0.0.2

License

MIT

Last publish

Collaborators

  • mattmcfarland