javascript - Passing data from Node js to HTML using EJS - Stack Overflow

Here's my Server Side code:app.set('view engine', 'html');app.engine('ht

Here's my Server Side code:

app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.get('/',function(req,res){
    var title = "PERFORMANCE OVERVIEW";
    res.render('Main.html',{name:title});
})

Here's my Client Side code(Main.html):

<div class="row">
    <div class="sm-col-12">
    <h3 contenteditable="true" style="font-family:BentonSans Medium; text-align:center; color:rgb(0,38,99);"><%= name %></h3>                    
    <hr style="border-top: dotted 2px;color:grey" />
    </div>
</div>

The output I am getting on Main.html Page is "<%-name%>". Instead, it should print "PERFORMANCE OVERVIEW". What exactly wrong in this code?

Edit:

I forgot to mention that I have also tried other variants like <%= name%> and {{ name }}. But I am getting the output "<%= name%>" and "{{ name }}" respectively.

Here's my Server Side code:

app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.get('/',function(req,res){
    var title = "PERFORMANCE OVERVIEW";
    res.render('Main.html',{name:title});
})

Here's my Client Side code(Main.html):

<div class="row">
    <div class="sm-col-12">
    <h3 contenteditable="true" style="font-family:BentonSans Medium; text-align:center; color:rgb(0,38,99);"><%= name %></h3>                    
    <hr style="border-top: dotted 2px;color:grey" />
    </div>
</div>

The output I am getting on Main.html Page is "<%-name%>". Instead, it should print "PERFORMANCE OVERVIEW". What exactly wrong in this code?

Edit:

I forgot to mention that I have also tried other variants like <%= name%> and {{ name }}. But I am getting the output "<%= name%>" and "{{ name }}" respectively.

Share edited Mar 3, 2019 at 17:48 Moshe Slavin 5,2145 gold badges26 silver badges39 bronze badges asked Mar 3, 2019 at 5:50 Harsh kumarHarsh kumar 871 gold badge2 silver badges9 bronze badges 3
  • Should that not be <%= name %> ? At a glance it looks like a typo where - is there instead of =. - means unescaped ( I think ), but I doubt you meant that – Neil Lunn Commented Mar 3, 2019 at 6:24
  • I have tried that also, but still getting the <%= name%> as output. Not the "PERFORMANCE OVERVIEW" – Harsh kumar Commented Mar 3, 2019 at 11:22
  • Did you get it working? If my answer didn't help can you update your code to show all the code on your app.js page? – Lord Elrond Commented Mar 3, 2019 at 17:27
Add a ment  | 

4 Answers 4

Reset to default 7

changing it to <%= name %> should fix it.

If that doesn't you can try changing app.set('view engine', 'html') to app.set('view engine', 'ejs');, then deleting the following 2 lines.

app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

app.get('/',function(req,res){
    var title = "PERFORMANCE OVERVIEW" ;
    res.render('Main',{name:title}) ;
});

I have always written it this way, so I can't say for sure if you syntax is correct, or if ejs will even work without setting it like this.

Update

Make sure the Main.html file is in a folder called views, and rename this file to Main.ejs.

next change res.render('Main.html',{name:title}); to res.render('main',{name:title});

<%- Outputs the unescaped value into the template
<%= Outputs the value into the template (HTML escaped)

As you are looking to print the value instead use <%= tag so change <%- name%> to <%= name%>

The information can be found here-https://ejs.co/#docs

Change <%- name%> to <%= name%> in your Main.html

const express = require('express');
const bodyParser = require('body-parser');

const port = 5000;

const app = express();

app.set('view engine', 'html');
app.engine('html', require('ejs').renderFile);
app.use(express.static('views'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.get('/', function (req, res) {
  var title = "PERFORMANCE OVERVIEW";
  res.render('Main.html', { name: title });
});

app.listen(port, console.log(`Server is running on port ${port}`))

Try this and it is rendering HTML page and also accessing the variable you pass in it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743685151a4490026.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信