karaxuna
karaxuna

Reputation: 26930

Serving combined JavaScript file

In html:

<script type="text/javascript" src="/layout/load_scripts?jquery.validate.min.js,jquery-1.5.1.min.js,jquery-ui-1.8.11.min.js,jquery.unobtrusive-ajax.min.js,jquery.validate.unobtrusive.min.js"></script>

on server:

load_scripts: function (context) {
    var combined_script = '';
    var script_names = context.parameters.split(',');
    for (var i = 0; i < script_names.length; i++) {
        combined_script += fs.readFileSync('./content/scripts/global/' + script_names[i]);
    }

    context.response.writeHead(200, { 'Content-Type': 'application/javascript' });
    context.response.end(combined_script);
}

But the script does not load. Please help

Upvotes: 1

Views: 75

Answers (2)

Pickels
Pickels

Reputation: 34632

Why would you use readFileSync for something like that? You could be reading all the files at the same time. I would probably solve this with the async library and have a more functional approach.

async.map(['file1','file2','file3'], fs.readFile, function(err, results){
   results.join(''); // This is your string.
});

More info on the Async GitHub page.

Upvotes: 1

Kristian
Kristian

Reputation: 21810

at first glance, i'd debug like this:

  1. write some static text and try printing that.
  2. confirm that fs.readFileSync() is actually reading files... ( printing to console should work )

And also, it doesn't look like you're returning your combined script anywhere.

Upvotes: 1

Related Questions