Questions
How to serve javascript file dynamically? Specifically, the scripts maintain most of its body but with some variables changable (imagine HTML Jade template, but this is for pure javascript).
Scenario
When user or browser (http GET in general) visits /file.js passing parameter api, e.g. /file.js?api=123456, I would like to output pure javascript where I can take that 123456 and put in inside of my code, dynamically. Content-Type is application/javascript.
Sample:
var api = #{req.query.api}; //Pseudo
//The rest of my javascripts template
...
From my main .js file, I have set up the route:
app.get( '/file.js', function( req, res ) {
//Pseudo code that I would like to achieve
var name = req.query.name;
res.render( 'out_put_javascript_file_from_jade_file.jade', { name: name } );
});
So when a person visits /file.js, the script file will be rendered differently based on the parameter api passed in the URL. The only possible dynamic way I can think of is using Jade, but it doesn't allow pure javascript template. I believe there must be other solutions.
Please excuse my explanation. The problem is somewhat like this: How to generate a pure JavaScript file with Jade
my_common.jsand generate inline JS in you JADE template. This inline JS would be the only JS which changes. But it's difficult to advice without more information :)http://example.com/file.js?api=123, the browser returnsapplication/javascriptwith the following codealert( 'Your API key is 123' );. Any method should work, but my limited knowledge doesn't come up with anything bright enough. Please advise.alert()? So tell us what do you want to do with the generated output? Who handle it / is processing it?alert()is pseudo code only. I'd like that when a route is visit, e.g./file.js?api=123, the browser will output pure javascript code that has part of its generated dynamically. PSEUDO: http.get/file.js?api=123will produceapi = 123; do_something_with_that_api();OR http.getfile.js?api=456will produceapi = 456; do_something_with_that_api();.