title: How to create html table dynamically from database with node.js and express?
body: <p>I am totally new in node.js and I want to create a simple phonebook app with express and postgresql. I want to have two pages, one to add a new contact and another one for contacts to be shown in an html table with the ability to update or delete rows. Until now I have implemented the insert but I don't know how to create the "contacts.html" page dynamically from database. Thank you in advance!</p>
  
  <p>index.html</p>
  
  <pre><code>&lt;header&gt;
      &lt;ul&gt;
          &lt;li&gt;&lt;h2&gt;Phonebook&lt;/h2&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="index.html" id="index"&gt;New Contact&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="contacts.html" id="contacts"&gt;Contacts&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/header&gt;
  
  &lt;section&gt;
      &lt;form action="insertContact"&gt;
          &lt;p&gt;Full Name&lt;/p&gt;
          &lt;input type="text" name="fullname" required&gt;
  
          &lt;p&gt;Phone&lt;/p&gt;
          &lt;input type="text" name="phone1" required&gt;
  
          &lt;p&gt;Mobile&lt;/p&gt;
          &lt;input type="text" name="phone2"&gt;
  
          &lt;p&gt;Address&lt;/p&gt;
          &lt;input type="text" name="address" required&gt; &lt;br&gt;&lt;br&gt;
  
          &lt;input type="submit" name="submitBtn" id="submitBtn" value="Submit"&gt;
      &lt;/form&gt;
  &lt;/section&gt;
  </code></pre>
  
  <p>server.js</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var db = require('pg');
  var http = require('http');
  
  var app = express();
  
  app.use(express.static(path.join(__dirname,'/')));
  
  var dbConnection = "postgres://postgres:root@localhost:5432/Phonebook";
  
  app.get('/insertContact',function(req,res){
      var dbClient = new db.Client(dbConnection);
  
      dbClient.connect(function(err){
          if(err)
              throw err;
  
          var query = "insert into Contacts (fullname,phone,mobile,address) values ($1,$2,$3,$4)";
          var fullname = req.query.fullname;
          var phone = req.query.phone1;
          var mobile = req.query.phone2;
          var address = req.query.address;
  
          var contact = [fullname , phone , mobile , address];
  
          dbClient.query(query , contact , function(err){
              if(err)
                  throw err;
              else {
                  console.log('Success!') ;
                  res.redirect('/');      
                  res.end();
              }               
          });
      });
  });
  
  app.get('????',function(req,res) {
      var dbClient = new db.Client(dbConnection);
  
      dbClient.connect(function(err){
          if(err)
              throw err;
  
          var query = "select * from Contacts";
  
          dbClient.query(query,function(err,result){
              if(err)
                  throw err;
              else {
  
                     ??????????
  
                  res.end();
              }
          });
      });
  });
  
  app.listen(8080,function(){
      console.log('Server started');
  });
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mLdBI.png" rel="noreferrer">sample image</a></p>