Nathan Loisel
Nathan Loisel

Reputation: 1

Send NodeJS variable to JavaScript

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

Upvotes: 0

Views: 104

Answers (1)

Fernando Matos
Fernando Matos

Reputation: 11

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!

Upvotes: 1

Related Questions