title: How to pass variable from node.js to html
body: <p>I am a newbie of node.js developer</p>
  
  <p>I am currently writing a program with below program structure:</p>
  
  <pre><code>view
   |
   |--- booking.handlebars (HTML program)
   |
  routes
   |
   |--- booking.js (node.js JavaScript program)
   |
  </code></pre>
  
  <p>When users access "dashboard" page, "booking.js" will connect to mongodb to retrieve all the "bookings" records from the Database "Booking". Then, my target is to pass the fetched "bookings" object to "booking.handlebars" HTML file.</p>
  
  <p>However, I cannot achieve this goal.</p>
  
  <p>I have thought many alternatives, including searching the threads from stackoverflow, using global variable; generating JSON file and then read the JSON file back, etc. But it seems doesn't work.</p>
  
  <p>I would like to seek your assistance that how can I pass all the object values from JavaScript program to HTML.</p>
  
  <p>Below is my code:</p>
  
  <p>booking.handlebars (HTML program)</p>
  
  <pre><code>&lt;div&gt;
      &lt;div&gt;
          &lt;table&gt;
              &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;td&gt;{{user.username}}&lt;/td&gt;
                      &lt;td&gt;{{booking}}&lt;/td&gt;
                      &lt;td&gt;{{req.booking}}&lt;/td&gt;
                      &lt;td&gt;{{bookingRecord}}&lt;/td&gt;
                      &lt;td&gt;{{bookings.booking}}&lt;/td&gt;
  
                  &lt;/tr&gt;
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>booking.js (node.js JavaScript program)</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var bookingRecord = null;
  
  
  router.get('/dashboard', function(req, res){
      res.render('dashboard');
  });
  
  
  router.post('/dashboard',
    function(req, res) {
      var MongoClient = require('mongodb').MongoClient;
      var url = "mongodb://localhost:27017/";
  
      MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var dbo = db.db("booking");
        dbo.collection("bookings").find().toArray(function(err, booking) {
          if (err) throw err;
          console.log(booking);
          bookingRecord = booking;
          db.close();
        });
      });
  
      res.redirect('/users/dashboard');
    });
  
  
  module.exports = router;
  </code></pre>