adspace


How to send data from node js to html?

Answer Posted / Devendra Dangwal

To send data from Node.js to HTML, you can use Express.js and EJS (Embedded JavaScript) templates. First, install EJS using npm: `npm install ejs`. Then create an 'views' folder in your project directory and create an 'index.ejs' file inside it with your HTML structure. To render the HTML and pass data, update 'app.js':
```javascript
const express = require('express');
const app = express();
app.set('view engine', 'ejs');

app.get('/', function(req, res) {
const data = { message: 'Hello World!' };
res.render('index', data);
});

app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is node js losing popularity?

592


How we can read a file in node js?

572