title: Node.js: Inserting a line after a particular line in a text file?
body: <p>I was trying to build a command line application, so that to block an ip (or whatever) I need not to update the conf file. For this, I need to insert a particular line of code with the ip at a particular place in the conf file. I know the CLI part. Can anybody help me with the inserting part?
  I wrote the code as follows, but is writing some gibberish!!</p>
  
  <pre><code>#!/usr/bin/env node
  var cli = require('cli');
  var fs = require('fs');
  cli.parse({
      block:   ['b', 'Block the ip:port'],
      ip: ['i', 'provide ip address', 'string', ''],
      port: ['p', 'provide port number', 'int', 0],
  });
  
  cli.main(function(args, options)
  {
      if(options.block)
      {
         fs.readFile('nginx.conf', function(err, data) {
            if(err) throw err;
            data = data.toString();
            var array = data.toString().split("\r\n");
  
            array[4] ='deny ' + cli.getValue(options.ip) + ':' + cli.getValue(options.port);
            for(i = 0; i &lt; array.length ; i++) {
              fs.writeFile('nginx1.conf', array[i]+'\r\n', function () {})
            };
         });
      }
  });
  </code></pre>