0

I would like to send variable (name) from NodeJS to HTML/JS on page loading (to get res parameter in HTML). But my code isn't working:

NodeJS part:

const fs = require('fs');
const https = require('https');
const path = require('path');
const directoryToServe = 'client';
const express = require('express');
const app = express();
var session = require('express-session');
app.use(express.static(path.join(__dirname, '/client')));
app.get('/', function(req, res) {
    res.render("index", { name: "example" });
});

HTML part:

<!DOCTYPE html>
<html>
<body>
<h1>{{ name }}</h1>
<p>My first paragraph.</p>
</body>
</html>

Of course, I have installed Handlebars. Thank you very much Nathan

6
  • Do you get an error of some sort? Commented Nov 12, 2018 at 19:38
  • Not in browser console, and in NodeJS console, it just write "{{ name }}" Commented Nov 12, 2018 at 19:39
  • Having it installed is not enough, it should be configured to be used by Express. The code shows that it isn't. Commented Nov 12, 2018 at 19:39
  • Have you any example code of how to configure it ? Thank you :) Commented Nov 12, 2018 at 19:41
  • This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express Commented Nov 12, 2018 at 19:43

1 Answer 1

1

You haven't imported express-handlebars or configured the Render Engine.

Example below:

const express = require('express');
const exphbs = require('express-handlebars');
const app = express();

app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

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.