In our project we have got client-side JavaScript hosted on Node.js.
This script uses another static content from server (something like http://host:port/static/content
). We can't use a local path, because this JavaScript could be injected in different sites.
Our development, test and production environments have different host and port settings. So we have to manualy hard-code these attributes for the different environments.
The easiest way to resolve this problem is to manually parse JavaScript as a string on the server side while preparing a response for the static file.
I think this is not a good idea.
Is there any built-in mechanism in Node.js for this case?
P.S. Sorry for my English.
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.