title: How to output result of a mysql query as a json file in node.js
body: <p>What i have her is this </p>
  
  <pre><code>app.post('/login', function (req, res){
    connection.connect();
    email = req.body.email;
    password = req.body.password;
    if(email &amp;&amp; password ) {
      console.log(email);
      console.log(password);
     // connection.query('SELECT username FROM user WHERE email =? and password = ?', [email, password], 
     // function (error, rows, fields) { 
      connection.query('SELECT * FROM user ', 
      function (error, rows, fields) {
        var str='';
        for (var i = 0;i &lt; rows.length; i++) {
          str = str + rows[i].username;
          res.end(str);
        }
        connection.end(); 
      }); 
    }
  });
  </code></pre>
  
  <p>so instead of just displaying the result i want it to be something like this:</p>
  
  <pre><code>{
   "username": "ABC",
   "username": "DEF",
   "username": "HIJ"
  }
  </code></pre>