Send NodeJS variable to JavaScript












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










share|improve this question






















  • Do you get an error of some sort?
    – zfrisch
    Nov 12 at 19:38










  • Not in browser console, and in NodeJS console, it just write "{{ name }}"
    – Nathan Loisel
    Nov 12 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.
    – estus
    Nov 12 at 19:39










  • Have you any example code of how to configure it ? Thank you :)
    – Nathan Loisel
    Nov 12 at 19:41










  • This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
    – Sello Mkantjwa
    Nov 12 at 19:43
















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










share|improve this question






















  • Do you get an error of some sort?
    – zfrisch
    Nov 12 at 19:38










  • Not in browser console, and in NodeJS console, it just write "{{ name }}"
    – Nathan Loisel
    Nov 12 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.
    – estus
    Nov 12 at 19:39










  • Have you any example code of how to configure it ? Thank you :)
    – Nathan Loisel
    Nov 12 at 19:41










  • This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
    – Sello Mkantjwa
    Nov 12 at 19:43














0












0








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










share|improve this question













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







javascript html node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 19:34









Nathan Loisel

15




15












  • Do you get an error of some sort?
    – zfrisch
    Nov 12 at 19:38










  • Not in browser console, and in NodeJS console, it just write "{{ name }}"
    – Nathan Loisel
    Nov 12 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.
    – estus
    Nov 12 at 19:39










  • Have you any example code of how to configure it ? Thank you :)
    – Nathan Loisel
    Nov 12 at 19:41










  • This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
    – Sello Mkantjwa
    Nov 12 at 19:43


















  • Do you get an error of some sort?
    – zfrisch
    Nov 12 at 19:38










  • Not in browser console, and in NodeJS console, it just write "{{ name }}"
    – Nathan Loisel
    Nov 12 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.
    – estus
    Nov 12 at 19:39










  • Have you any example code of how to configure it ? Thank you :)
    – Nathan Loisel
    Nov 12 at 19:41










  • This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
    – Sello Mkantjwa
    Nov 12 at 19:43
















Do you get an error of some sort?
– zfrisch
Nov 12 at 19:38




Do you get an error of some sort?
– zfrisch
Nov 12 at 19:38












Not in browser console, and in NodeJS console, it just write "{{ name }}"
– Nathan Loisel
Nov 12 at 19:39




Not in browser console, and in NodeJS console, it just write "{{ name }}"
– Nathan Loisel
Nov 12 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.
– estus
Nov 12 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.
– estus
Nov 12 at 19:39












Have you any example code of how to configure it ? Thank you :)
– Nathan Loisel
Nov 12 at 19:41




Have you any example code of how to configure it ? Thank you :)
– Nathan Loisel
Nov 12 at 19:41












This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
– Sello Mkantjwa
Nov 12 at 19:43




This link should help: kulik.io/2018/01/02/how-to-use-handlebars-with-express
– Sello Mkantjwa
Nov 12 at 19:43












1 Answer
1






active

oldest

votes


















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!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268935%2fsend-nodejs-variable-to-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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!






    share|improve this answer




























      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!






      share|improve this answer


























        1












        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!






        share|improve this answer














        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!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 21:03









        bobbyrne01

        2,11343993




        2,11343993










        answered Nov 12 at 20:55









        Fernando Matos

        111




        111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268935%2fsend-nodejs-variable-to-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine