Note that there are some explanatory texts on larger screens.

plurals
  1. PONode Streaming, Writing, and Memory
    primarykey
    data
    text
    <p>I'm attempting to dynamically concatenate files prior to serving their content. The following very simplified code shows an approach:</p> <pre><code>var http = require('http'); var fs = require('fs'); var start = '&lt;!doctype html&gt;&lt;html lang="en"&gt;&lt;head&gt;&lt;script&gt;'; var funcsA = fs.readFileSync('functionsA.js', 'utf8'); var funcsB = fs.readFileSync('functionsB.js', 'utf8'); var funcsC = fs.readFileSync('functionsC.js', 'utf8'); var finish = '&lt;/script&gt;&lt;/head&gt;&lt;body&gt;some stuff here&lt;/body&gt;&lt;/html&gt;'; var output = start + funcsA + funcsB + funcsC + finish; http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(output); }).listen(9000); </code></pre> <p>In reality, how I concatenate might depend on clues from the userAgent. My markup and scripts could be several hundred kilobytes combined.</p> <p>I like this approach because there is no file system I/O happening within createServer. I seem to have read somewhere that this <code>response.write(...);</code> approach is not as efficient/low overhead as streaming data using an <code>fs.createReadStream</code> approach. I seem to recall this had something to do with what happens when the client cannot receive data as fast as Node can send it.(?) We seem to be able to create a readable stream from a file system object, but not from memory. Is it possible to do what I have coded above with a streaming approach? With file I/O happening initially, <em>outside of the CreateServer function</em>?</p> <p>Or, on the other hand, are my concerns not that critical, and the approach above offers perhaps no less efficiency than a streaming approach.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload