0

I have data in a text file that contains HTML with template literals like this

<html>
<head></head>
<body>
<p>`${abc}`</p>
</body>
</html>

I have data from the server abc.. I want node to read this data from the .txt file , replace template literals with the data and then append it to another .html file.. This is what I tried

var abc =  "Hi there"
var dat = fs.readFileSync('home.txt', 'utf8');
fs.writeFile('index.html', dat, (err) => {
     if (err) throw err;
        console.log('The file has been saved!');
    });

Where am i going wrong?

Saikiran
  • 756
  • 2
  • 11
  • 29
  • Use [a template engine](https://expressjs.com/en/guide/using-template-engines.html) – lumio Sep 22 '17 at 08:16
  • You mean use .ejs instead of .txt? – Saikiran Sep 22 '17 at 08:18
  • 1
    You can send abc string to the client and in your javascript file, you can get the string and append it to a DOM element. – Симеон Симеонов Sep 22 '17 at 08:21
  • 1
    ejs is one of many. [Here](https://scotch.io/tutorials/use-ejs-to-template-your-node-application) is a good tutorial on how to use ejs and [here](http://blog.teamtreehouse.com/getting-started-with-handlebars-js) how to use handlebars (another template engine). Basically what you wanna do is to tell the template engine what to replace in your template file. – lumio Sep 22 '17 at 08:22
  • Ok will check it out – Saikiran Sep 22 '17 at 08:30
  • @lumio I need to download the html that is created in the folder. Not to render it. Is it still the way to go? AFAIK .ejs is used to render. Is it still the way to go about it? – Saikiran Sep 22 '17 at 08:34
  • 1
    When you want to save the HTML file with the replaced strig, you need to render and then save the result :) – lumio Sep 22 '17 at 08:36
  • I see. My approach was to validate the data directly from the .txt and save it to a .html file and make it downloadable. Will try your idea then, seems to be better than mine – Saikiran Sep 22 '17 at 08:38
  • @lumio Is it possible to download the rendered html from the node itself? My current app requires the client to be able to download the file as html. – Saikiran Sep 22 '17 at 08:42
  • 1
    Yes, you just need to set the right response headers and the browser will download your file instead of rendering it. Look at [this answer](https://stackoverflow.com/a/20509354/881032) about the response headers. And please edit your question, providing all that information we discussed here, so someone can write an answer :) Also keep [this article (mcve)](https://stackoverflow.com/help/mcve) in mind – lumio Sep 22 '17 at 08:45
  • Thanks a lot, that helped – Saikiran Sep 22 '17 at 08:46
  • Content-Type: application/octet-stream Content-Disposition: attachment; filename="index.html" – Saikiran Sep 22 '17 at 09:29
  • Where do I write this? In the header of the .ejs file or is it an argument in the res.render() method? – Saikiran Sep 22 '17 at 09:30

1 Answers1

1

It is better to use templating engine such as Mustache. For example:

Your txt file:

<html>
<head></head>
<body>
<p>{{abc}}</p>
</body>
</html>

And your code:

const Mustache  = require('mustache')

var abc =  "Hi there"
var dat = fs.readFileSync('home.txt', 'utf8');
var html = Mustache.render(dat, {abc: abc});
fs.writeFile('index.html', html, (err) => {
 if (err) throw err;
    console.log('The file has been saved!');
});
Tolsee
  • 1,695
  • 14
  • 23