2

私たちのプロジェクトでは、Node.jsでホストされているクライアント側のJavaScriptを使用しています。

このスクリプトは、サーバーからの別の静的コンテンツ(のようなものhttp://host:port/static/content)を使用します。このJavaScriptは別のサイトに挿入される可能性があるため、ローカルパスを使用することはできません。

当社の開発、テスト、および実稼働環境では、ホストとポートの設定が異なります。したがって、さまざまな環境に対してこれらの属性を手動でハードコーディングする必要があります。

この問題を解決する最も簡単な方法は、静的ファイルの応答を準備するときに、サーバー側でJavaScriptを文字列として手動で解析することです。

これは良い考えではないと思います。

この場合、Node.jsに組み込みのメカニズムはありますか?

PS私の英語でごめんなさい。

4

1 に答える 1

0

I'm assuming here that the client-side code is invoked from some dynamically-generated HTML.

If the amount of "configuration" data is fairly small, you could pass it via data-* attributes in the generated HTML, e.g. <body data-static="http://host:port/static/content"> or, if it's bigger, in an inline <script id="config" type="application/json"> element.

The key here is to avoid dynamically generating code whose only purpose is to represent data (40 years ago Kernighan and Plauger, in The Elements of Programming Style wrote that they wondered just how much much of the daily activity in a typical computer center amounted to simply entering parameters via the compiler). With an interpreted language it's easy for the code/data distinction to get blurred, but it's worth keeping them separate in your mind.

P.S. Your English is better than that in most questions I've seen.

于 2012-08-17T02:22:26.757 に答える