Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Node.js以HTML格式打印Mysql数据_Node.js - Fatal编程技术网

使用Node.js以HTML格式打印Mysql数据

使用Node.js以HTML格式打印Mysql数据,node.js,Node.js,我现在正在学习Node.js,但我有点困惑 我正在使用以下代码接收数据: var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'db' }); connection.connect(); connection

我现在正在学习Node.js,但我有点困惑

我正在使用以下代码接收数据:

var mysql      = require('mysql');  
var connection = mysql.createConnection({  
  host     : 'localhost',  
  user     : 'root',  
  password : '',  
  database : 'db'  
});  

connection.connect();  

connection.query('SELECT * FROM users', function(err, rows, fields)   
{  
  if (err) throw err;  

  console.log(rows[0]);  

});  

connection.end();  
我的调试控制台正在显示数据库表的第一行,但我想在HTML页面中打印此值。我该怎么做

谢谢。

您需要查看以将这些结果发送到html站点

var express = require('express');
var app = express();

app.use(express.static('public'))

var mysql      = require('mysql');  
var connection = mysql.createConnection({  
  host     : 'localhost',  
  user     : 'root',  
  password : '',  
  database : 'db'  
});        

app.get('/rows', function (req, res) {
  connection.connect();  

  connection.query('SELECT * FROM users', function(err, rows, fields)   
  {  
      connection.end();

      if (err) throw err;  

      res.json(rows); 

  });
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
运行此文件将在计算机的端口3000上创建服务器

在项目中,在根级别创建名为
public
的目录。在那里,您可以创建一个名为
index.html
的文件,其内容如下:

<html>
    <head>
        <title>My db rows</title>
    </head>

    <body>
        <div id="table"></div>

        <script type="text/javascript">
             var opts = {
                 url: 'http://localhost:3000/rows/'
             };

             fetch(opts)
                 .then((res) => {
                     if (res.ok) {
                         return res.json();
                     }
                  })
                  .then((rows) => {
                      for (let row of rows) {
                          // row will be a mysql row -- you can loop over these and do what you want with them
                      }
                   })
                   .catch(console.log);
        </script>
    </body> 
</html>

我的数据库行
变量选项={
网址:'http://localhost:3000/rows/'
};
取回(opts)
。然后((res)=>{
如果(res.ok){
返回res.json();
}
})
。然后((行)=>{
for(让一行中的一行){
//行将是一个mysql行——您可以在这些行上循环,并对它们执行您想要的操作
}
})
.catch(console.log);
注意:我正在使用新的FetchAPI来执行我的请求,但是您可以同样轻松地使用jquery或XHR。可以更详细地解释fetchapi

然后导航到
http://localhost:3000/index.html
在浏览器中查看结果