3

I am using express on a nodejs platform and serve my javascript library files using this directive

app.use('/static', express.static(__dirname + '/static'))

Now I want to be able to write something like that, inside one of these static files

[..] var host = <%= host %> [..]

Is there any possibility to expand variables inside statically served files?

Coxer
  • 1,694
  • 2
  • 26
  • 44
  • 2
    Isn't it a bit contradictory to want to serve a _static_ page, but at the same time do variable expansion ? Why not simply use one of the template engines available ? – Luc Morin Nov 02 '13 at 18:20
  • How would you transport a localized string into a js file? – Coxer Nov 03 '13 at 16:22
  • I see. You want to render your javascript files dynamically. The question is, does the localized string need to be defined in the javascript file itself, or can't you pass it as a template variable to your "view" file ? – Luc Morin Nov 03 '13 at 17:01
  • I have a ejs template, which includes a page specific js file. The js file displays a dialog with a localized string. Currently I am printing this string to a javascript variable inside the ejs file and access it in the js file. But I think this is not very clean ... – Coxer Nov 03 '13 at 17:13

3 Answers3

2

Here's an idea you might want to refine:

var ejs = require('ejs');

//Express.js setup
...

app.use('/public/js/myfile.js', function(req, res){
    var f = ejs.compile('var host = <%= hostname %>;');
    var fileContent = f({hostname: 'somevalue'});
    res.setHeader('Content-Type', 'application/javascript');
    res.setHeader('Content-Length', fileContent.length);
    res.send(fileContent);
});
app.use(express.static(path.join(__dirname, 'public')));

//Routes definition
...

I used ejs since that's what you already use. So the idea is to have a middleware that catches requests to those "dynamic" javascript files, and use the template engine to compile the content.

Note that I use a simple string variable here, but you could read the file from disk and then compile it.

Cheers

Luc Morin
  • 5,302
  • 20
  • 39
0

You can use loDash's templating with express using lodashinexpress.

musically_ut
  • 34,028
  • 8
  • 94
  • 106
  • Yeah, sure. I am currently using ejs templating, but I am serving only partial html files on my static folder. And now I was wondering if I can achieve the variableexpansion by using special endpoints for that files and use .render inside these or is there another way? – Coxer Nov 02 '13 at 10:41
0

Please see my answer here: https://stackoverflow.com/a/19812753/2728686

Using the Jade templating engine you can serve static files and send serverside javascript variables to the static HTML files (or to any jade templates), as such:

Server side (server.js):

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.get('/', ensureAuthenticated, function(request, response){
    response.render('index', { data: {currentUser: request.user.id} });
});

app.use(express.static(__dirname + '/www'));

views/index.jade:

!!! 5
script(type='text/javascript')
    var local_data =!{JSON.stringify(data)}

include ../www/index.html
Community
  • 1
  • 1
AmpT
  • 2,146
  • 1
  • 24
  • 25