title: Populate javascript file with variable value in node.js
body: <p>I use <code>node.js</code>, <code>express.js</code> and <code>pug</code>/<code>jade</code>.</p>
  
  <p>In my server I populate a view with a variable <code>pageId</code>.</p>
  
  <p>I use this variable in my <code>pug</code> view with</p>
  
  <pre><code>script.
    document.addEventListener('DOMContentLoaded', function () {
      var pageId = '#{pageId}';
  
      (...)
  </code></pre>
  
  <p>The problem is that I want this script code to be located in a separate file rather than in my view.</p>
  
  <p>So I want to include the script with</p>
  
  <pre><code>script(src='js/my_script.js')
  </code></pre>
  
  <p>but then I am not able to get the value of the variable in the script.</p>
  
  <p>How is this normally done? It seems wrong to send variables from the server to a script code in the view on the client side.</p>
  
  <p>I guess it is irrelevant that I use <code>pug.js</code>, since the same problem must arise even in plain html.</p>