【问题标题】:Get data from MySQL using Node.js and display it on index page使用 Node.js 从 MySQL 获取数据并显示在索引页面上
【发布时间】:2018-10-15 15:39:25
【问题描述】:

每当我在浏览器上运行我的网站时,我都会收到以下错误消息:http://localhost:3000。我正在努力解决这个问题。有没有其他方法或更好的方法从 MySQL 获取数据并使用 Node.js 将其显示在我的索引页面上?

错误

Error: Failed to lookup view "index" in views directory "C:\test\views"

index.ejs

<!DOCTYPE html>
<html>
    <head>
        <title>MySQL Data</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    </head>
    <body>
        <table id="tblData" class="table table-bordered table-hover">
            <thead class="thead-inverse">
                <tr>
                    <th data-column-id="Name">
                        Name
                    </th>
                    <th data-column-id="Message">
                        Message
                    </th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
        <script>
            //Declare the var with the data
            var Result = <%- JSON.stringify(result) %>;

            var TableData = null;
            $(function () {
                function ajusTables() {
                    $('.table').css('width', '100%');
                    $('.dataTables_scrollHeadInner').css('width', '100%');
                }

                TableData = $('#tblData').DataTable({
                    "serverSide": false,
                    "drawCallback": function (settings) {
                        ajusTables();
                     },
                     "scrollX": true,
                     "processing": true,
                     "paging": true,
                     "lengthChange": false,
                     "searching": true,
                     "ordering": true,
                     "info": true,
                     "autoWidth": true,
                     "deferRender": true,
                     "columns": [
                         { "data": "Name" },
                         { "data": "Message" },
                     ],
                     "order": [0, "asc"]
                });
                console.log(Result);
                TableData.rows.add(Result)
                TableData.draw();
            });
        </script>
    </body>
</html>

我安装了 EJS 包,在 html 中使用 Javascript 并从后端操作数据。

res.render("Index", {result: result}); 将调用 Index.ejs,传递带有数据的 JSON。

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 + '/'));
app.set("view engine", "ejs");

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

connection.connect();

app.get('/', function (req, res) {
    connection.query('SELECT * FROM chat', function(err, result, fields) {  
        connection.end();
        if (err) throw err;
        console.log("Data displayed");
        res.render("index", { result: result });
    });
});

app.listen(3000, function () {
    console.log('Connected to port 3000');
});

【问题讨论】:

  • 错误说它在C:\test\views 中找不到索引视图所以问题是你在C:\test\views 中有你的index.ejs 吗?
  • 不,它在C:\test。我没有名为 views 的文件夹
  • 所以创建它并将文件放在那里
  • 现在我得到这个错误:Error: Failed to lookup view "index" in views directory "C:\test\views\views"
  • 您是否更改了代码中的任何内容? res.render("index"这行你改了吗?

标签: javascript jquery html mysql node.js


【解决方案1】:

检查以确保文件名为 index.ejs 而不是 Index.ejs(大小写)。

【讨论】:

    【解决方案2】:

    参考链接:https://www.codementor.io/naeemshaikh27/node-with-express-and-ejs-du107lnk6

    请在 views 文件夹中创建 index.ejs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多