使用nodejs服务动态javascript文件 [英] Serve dynamic javascript file with nodejs

查看:79
本文介绍了使用nodejs服务动态javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何动态提供javascript文件?具体来说,这些脚本保留了其大部分内容,但具有一些可变的变量(可以想象HTML Jade模板,但这仅适用于纯JavaScript).

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).

场景

当用户或浏览器(通常为http GET)访问/file.js 并传递参数 api 时,例如/file.js?api=123456 ,我想输出纯JavaScript,在其中可以将 123456 动态地放入我的代码中.内容类型为 application/javascript .

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.

示例:

var api = #{req.query.api}; //Pseudo
//The rest of my javascripts template
...

在我的主要.js文件中,我设置了路由:

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 } );

});

因此,当一个人访问/file.js 时,脚本文件将根据URL中传递的参数 api 进行不同的呈现.我能想到的唯一可能的动态方式是使用Jade,但它不允许使用纯JavaScript模板.我相信必须有其他解决方案.

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.

请原谅我的解释.问题有点像这样:如何生成纯JavaScript文件与翡翠

Please excuse my explanation. The problem is somewhat like this: How to generate a pure JavaScript file with Jade

推荐答案

如果您想快速又肮脏地做某事,那么您可以做这样的事(基于注释中的示例).

If you want to do something quick and dirty, then you can do something like this (based on your example in the comments).

应用程序初始化-读取.js template 文件并将其缓存:

App init - read the .js template file and cache it:

// this should be async, but hey, not teaching you that part here yet
var fileJs = fs.readFileSync('file.js.template');

File.js:

(function() {
  $(window).on('load', function() {
    alert('Your api key is API_KEY_CONST');
  });
})();

请求:

GET /api/file.js?key=123

路由器:

app.get('/api/file.js', function(req, res) {

    var key = req.query.key;
    var key = fetchKeyFromDBSync(); // just to make it easier here, no async.
    var out = fileJs.replace(API_KEY_CONST, key);

    res.setHeader('content-type', 'text/javascript');
    res.write(out);
    res.end();
});

现在,这真的很愚蠢,您不应该在家中尝试它,而只是演示了如何做自己想做的事情.

Now, this is really dumb and you should not try it at home, but it simply demonstrates how to do what you wanted.

根据文件的长度,如果将文件的块放入数组中,则性能可能会更好一些,例如:

Depending on the file length, you might perform a bit better if you put the chunks of the file into an array, like:

var fileChunks = ['(function(){ blablabla;', 'var myAPIKey=', 'KEY_PLACEHOLDER', '; alert (myAPIKey);', '})()']

因此,稍后使用真正的API密钥解析它时,就可以加入文件.

So later when you're resolving it with the real API key, you join the file.

fileChunks[2] = '12345';
var responseData = fileChunks.join('');
res.write(responseData);

但是您最后访问的api密钥然后保存在一个数组中.并不是很可靠的未来证明,但是如果您需要快速的帮助,它将很有用.

But your last-accessed api key is then held in an array. Not quite future proof, but it shouls work if you need something quick.

这篇关于使用nodejs服务动态javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆