0

Here is a problem I am having when using Node.js:

This is my code in index.js:

.....
app.get('/xyz', function(request, response) {
  fs = require('fs');
  fs.readFile('views/pages/xyz.ejs', 'utf8', function (err,data) {

  if (err) {
    return console.log(err);
  }

  response.render('pages/xyz', {result:data});
  });
});
.....

Here is xyz.ejs:

<!DOCTYPE html>
<html>
<head>
</head>

<body>

<div class="container">

<% displayStr=result.replace("\n", "<br/>"); %>
<%= displayStr %>

</div>

</body>
</html>

Here is what I get in my browser, when I point it to top/yxz :

<!DOCTYPE html><br/><html> <head> </head> <body> <div class="container"> <% displayStr=result.replace("\n", "<br/>"); %> <%= displayStr %> </div> </body> </html>

Even though there are line break tags (<br/>), I get a one long line text display instead of an HTML display. I tried various things I could find, but nothing worked.

What should I do to obtain a normal display line by line?

2
  • @mplungian Thanks for the edit. For some reason I couldn't get it right. Commented Nov 15, 2016 at 10:20
  • Can xyz.ejs be changed itself? Commented Nov 15, 2016 at 12:19

2 Answers 2

2

index.js:

...
response.render('pages/xyz', {result:'<xmp>'+data+'</xmp>'});
...

xyz.ejs: (Unescaped buffering with <%- code %>)

...
<% displayStr=result.replace("\n", "<br/>"); %>
<%- displayStr %>
...
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't have to read manually the file.

Can you try:

app.get('/xyz', function(request, response) {
  response.render('pages/xyz');
});

Of course, before you should:

app.set('view engine', 'ejs');

1 Comment

No, no. I have to. My purpose here is to display its contents. Otherwise it is working with no problems. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.