Reputation: 6039
Here is what I have in my server.js
:
var browserify = require('browserify-middleware');
var express = require('express');
var app = express();
var path = require("path");
app.listen(8080);
console.log('Listening at http://localhost:8080');
// routes will go here
app.get('/render', function(req, res) {
res.sendFile(path.join(__dirname+'/public/index.html'), {text: "sfsdfsf"});
});
and here are the ways I tried to get the variable text
in my index.html
, althgouh none worked:
<h1>{{ text }} </h1>
<h1><% text %> </h1>
<script type="text/javascript">
var text = "<%= text %>";
var text2 = "{{ text }}";
</script>
Any thoughts where I am going wrong?
Upvotes: 3
Views: 1863
Reputation: 586
Option1: I would suggest to add template engine like pug. Template engines replace the variables in a template file with actual values. To implement pug template, visit https://expressjs.com/en/guide/using-template-engines.html
Option 2:
'use strict';
var express = require('express');
var path = require("path");
var app = express();
const template = (text) => {
return `
<!DOCTYPE html>
<html>
<head>
<title>sample</title>
</head>
<body>
<h1>${text}</h1>
</body>
</html>`;
}
app.get('/render', function(req, res) {
res.send(template('Hello'));
});
app.listen(8080);
console.log('Listening at http://localhost:8080');
Upvotes: 1