MySQL数据不会显示在我的HTML表格上 - Node.js

时间:2018-05-05 13:40:34

标签: javascript html mysql node.js express

下面的代码确实有效,但我在MySQL中的数据没有显示在浏览器的HTML表格中:http://localhost:3000。我正在努力解决这个问题。如果有人能帮助我,我将不胜感激。

的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) {   
                    console.log(rows);
                }                   
            })                   
                .catch(console.log);
    </script>
</body> 

app.js

var express = require('express');
var app = express();
var mysql = require('mysql'); 
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', express.static(__dirname + '/'));

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

app.get('/rows', function (req, res) {
    connection.connect();  
    connection.query('SELECT * FROM chat', 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!');
});

0 个答案:

没有答案