0

Using Node.js server with Reload for front-end development. Don't want to include reload.js-script loading in index.html. Trying to use Node-script for appending it via development server.

var request  = require('request');
var cheerio  = require('cheerio');
var fs       = require('fs');
var path     = require('path');
var $$       = cheerio.load(fs.readFileSync('index.html'));

request('index.html', function (error, response, html) {

  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html), text = [];  

    $$('head').append('<script src="/reload/reload.js"></script>');

    fs.writeFileSync(path.join(process.cwd(), 'index.html'), $$.html(), {'encoding': 'utf-8'});
 }
});

Append doesn't work but script doesn't show any errors.

IJK
  • 11
  • 1
  • 5
  • Hello, why are you loading 'index.html' content multiple times? You read file in `var $$ = cheerio.load(fs.readFileSync('index.html'));` and then `var $ = cheerio.load(html)` but you use the `$$` variable in your request handler. Don't forget to restart node.js or use nodemon because your first `$$` is going to be the same in memory until you run it again. – lazlojuly Oct 24 '15 at 11:23
  • Hey thanks for answer! I was using that as a startpoint for using cheerio and just didn't realize: http://stackoverflow.com/a/26967090/2269584 I'm using supervisor and also always testing with manual node.js restart: https://github.com/J-Kallunki/staticserver-reload – IJK Nov 06 '15 at 08:03

0 Answers0