0

server.js

app.get('/',(req,res) => {
let path = require('path');
res.sendFile(path.join(__dirname+'/index.html'),{name:"srk"})
})

index.html

<%= name %>

In some website i found this way to send data from express to html. but i am getting <%= name %> instead of actual data. please have a look into it

1 Answers1

0

You should use res.render( view, { options } ) instead i.e

  • You need a view engine such as ejs, and then set it in your code
  • Create views folder and point express to the folder
  • Rename your index.html file to index.ejs and move it to your newly created views folder

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

    app.set('views', <views folder url>);

Then in your app.get(), use

`res.render('index.ejs', {
    name: "srk"
}`

if you don't already have ejs, run npm install --save ejs

Deyosse
  • 1
  • 1
  • Yes, the code in ejs file is html. The only difference is the file extension .ejs instead of .html – Deyosse Oct 29 '18 at 17:09