使用来自MySQL数据库的NodeJ在html / js文件中显示数据

Nar*_*wat 5 html mysql node.js

我的index.js文件是

var express         = require('express');
var  app            = express();
var path            = require('path');
var router          = express.Router();
var data            = require('./data/jsonData');
var createDatabase  = require('./data/db');
var careateTable    = require('./data/createTable');
var insert          = require('./data/insert');
var bodyParser      = require('body-parser');
var select          = require('./data/select');


app.use(express.static(path.join(__dirname, 'www')));
app.use(express.static(path.join(__dirname, 'form')));
app.use(bodyParser());
app.get('/' , function (req , res) {
    res.sendFile(path.join(__dirname + '/www/index.html'));
});

app.get('/data' ,function (req , res) {
    res.json(data);
});
app.get('/form' ,function (req , res) {
    res.sendFile(path.join(__dirname + '/form/index.html'));
});
app.post('/form' ,function (req , res) {
    console.log(req.body.user);
    console.log(req.body.password);
    insert.insertModule(req.body.user , req.body.password);
    res.sendFile(path.join(__dirname + '/www/index.html'));
});
app.get('/show' , function (req , res) {
    var i ;
   select.select( function (err, results) {
       if (err == 'error') {
           console.log(err);
       } else {
           console.log(results);
           res.send(results.username);
       }
   });

});

app.listen(3000);
console.log("App is listning on port 3000");
Run Code Online (Sandbox Code Playgroud)

并且select.js是

var mysql = require('mysql');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "NodeDataBase"
});
con.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");
});

module.exports  = {
    select: function (callback) {
        var sql = "SELECT username , password FROM login ";
        con.query(sql, function (err, result , fields) {
            if (err) {
                callback("error", err)
            } else {
                callback("success", result)
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我想将结果对象数据显示为html文件,所以我该怎么做呢建议我。

从获取请求/显示中,它将显示从数据库中获取的所有用户数据

Ale*_*ñoz 2

I\xc2\xb4ll 尝试解释它的工作方式(考虑到您现在可以在“ http://localhost:3000/show ”上看到数据)。如果我以错误的方式解释某些内容,请纠正我。

\n\n

在那里,你的代码中包含的是

\n\n
\n

服务器端代码

\n\n
    \n
  • mysql:声明与数据库的连接(这是您的数据库连接器)
  • \n
  • node.js:声明从数据库放入/推送/获取数据的方法(您的服务器端按原样)
  • \n
  • express.js:声明从数据库放入/推送/获取数据的 url(http/https 路由器)
  • \n
\n
\n\n

然后,如果我们检查代码,我们可以看到服务器 api 的声明 - 例如app.get(\'/show\')

\n\n

在那里,你所说的是express,将使用url /show和方法GET

\n\n

这是您的客户出现在场景中的地方。现在,我们假设您的服务器已启动并正在运行,正在等待为http://localhost:3000/show上的 GET 请愿提供服务。

\n\n

如果这是正确的,当单击链接时,您应该看到用户名,现在您将需要一个 http 客户端来连接到您的 http 服务器端。

\n\n

从服务器获取 HTML 客户端数据的方法是 JavaScript。

\n\n

然后,您需要构建一个 HTML 文件,其中还包含一个javascript脚本(在我的示例中用Angular编写)。

\n\n

HTML(这是用jade编写的。你可以转换它)应该如下所示:

\n\n
\n

客户端 HTML 代码\n 您应该创建一个 index.html 文件,并粘贴此代码

\n
\n\n
<!doctype html>\n<html ng-app>\n  <head>\n    <title>My AngularJS App</title>\n    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>\n  </head>\n  <body>\n  <div ng-controller="MyCtrl">\n    <table>\n      <thead>\n        <tr>\n          <th>\n            <p>Name</p>\n          </th>\n        </tr>\n      </thead>\n      <tbody>\n        <tr ng-repeat="user in users">\n          <td> \n            <p>{{user}}</p>\n          </td>\n        </tr>\n      </tbody>\n    </table>\n   </div>\n    <script>\n     var myApp = angular.module(\'myApp\',[]);\n     function MyCtrl($scope, $http) {\n     //This method will call your server, with the GET method and the url /show\n     $http.get("http://localhost:3000/show").then(function(success){\n      if(success.data.length>0)\n      {\n         $scope.users=success.data;\n      }\n     });\n     }\n   </script>\n  </body>\n </html>\n
Run Code Online (Sandbox Code Playgroud)\n\n

此代码应捕获数据并在表中为数据库中的每个名称显示一行。

\n\n

希望它能有所帮助,至少可以清楚地解释完整的堆栈(客户端-服务器)如​​何工作。

\n