Sunday, 14 May 2017

Node.js Output Variation Between console.log() and response.write()?

I have the following Node.js Code that displays the plain text content of a file of installed NPM Modules on both the console and the resulting web page:

var flargs = process.argv.slice(1);
var flnm = flargs[1]
//  Continue With This
flsys.readFile(flnm, {encoding: "utf8"}, function read(err, data) {
    if (err) {
        throw err;
    }
fltxt = data;
console.log(fltxt);  // Print Contents of File To Shell Display
});

var server = http.createServer(function (req, res) {
    res.statusCode = 200;
    res.setHeader('Content-type', 'text/plain');
    res.write(fltxt);
    res.end();
});

server.listen(port);
console.log('Server Started on Port #: ' + port);
console.log('Argument: ' + flnm);

The console.log command displays the proper text output. The HTTP Response Objects Encodes the Line Output Differently ... Sample Output on the Browser:

│ /usr/lib
│ 
├── abbrev@1.0.4
│   Like ruby's abbrev module, but in js
│   http://github.com/isaacs/abbrev-js
├── ansi@0.2.1
│   Advanced ANSI formatting tool for Node.js
│   git://github.com/TooTallNate/ansi.js.git
├── archy@0.0.2
│   render nested hierarchies `npm ls` style with unicode pipes
│   http://github.com/substack/node-archy.git
├── asn1@0.1.11
│   Contains parsers and serializers for ASN.1 (currently BER only)
│   git://github.com/mcavage/node-asn1.git
├── assert-plus@0.1.4
│   Extra assertions on top of node's assert module
│   https://github.com/mcavage/node-assert-plus.git
├── async@0.2.10
│   Higher-order functions and common patterns for asynchronous code
│   https://github.com/caolan/async.git
├── aws-sign@0.3.0
│   AWS signing. Originally pulled from LearnBoost/knox, maintained as    vendor in request, now a standalone module.
│   https://github.com/mikeal/aws-sign
├─┬ block-stream@0.0.7
│ │ a stream of blocks
│ │ git://github.com/isaacs/block-stream.git
│ ├── UNMET DEPENDENCY inherits *
│ └── inherits@2.0.0 (inherits@2) invalid
│     Browser-friendly inheritance fully compatible with standard   node.js inherits()
│     https://github.com/isaacs/inherits

How Can I resolve the encoding difference on the Browser Text Output?



via Anthony Dycks

No comments:

Post a Comment