使用 Node.js 从 MySQL 中提取数据并显示在 HTML 页面上 [英] Extract data from MySQL using Node.js and display on HTML page

查看:46
本文介绍了使用 Node.js 从 MySQL 中提取数据并显示在 HTML 页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 MySQL 中提取数据并将其显示在我的 HTML 页面上,但是当我在我的浏览器 http://localhost:3000 上运行下面的代码时,数据没有显示在我的页面.如果有人能帮我解决这个问题,我将不胜感激.

I am trying to extract data from MySQL and display it on my HTML page, but when I run the code below on my browser http://localhost:3000, the data does not display on my page. I would appreciate if somebody could help me solve this problem.

index.html

<!DOCTYPE>
<html>
    <head>
        <title>Data from MySQL</title>
    </head>
    <body>
        <div id="output_message"></div>
    </body>
</html>

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', 'html');

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

connection.connect();

app.get('/',(req, res) => {
    connection.query("SELECT * FROM chat",(err, result) => {
        if(err) {
            console.log(err); 
            res.json({"error":true});
        }
        else { 
            console.log(result); 
            res.json(result); 
        }
    });
});

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

推荐答案

您需要考虑多个因素:

1 - 当您想使用通过 HTML 文件发送的 POST 方法时,您必须有一个

1 - When you want to use a POST method sent through a HTML file you must have a

<form action="Name of your processing file" method="POST">YOUR FORM HERE</form>

然而;如果您只是尝试调用数据库并获取数据,则只需执行

However; if you simply trying to call a db and get your data then just perform

您在 GET 方法中的逻辑如下:

your logic within the GET method as following:

app.get('/',(req, res) => {
    connection.connect(function(err) {
    if(err) throw err;
        else {
            connection.query("SELECT * FROM chat",(err, result) => {
                if(err) {
                    console.log(err); 
                    res.json({"error":true});
                }
                else { 
                    console.log(result); 
                    res.json(result); 
                }
            });
        }
    });
});

*** 你可能会问 index.html 怎么样.对此有两种回应:

*** You may ask what about the index.html . There are two responses for that :

1 - 当你想发送文件时使用 res.sendFile(__dirname + "/YOURPAGE-Name.html");

1 - Use res.sendFile(__dirname + "/YOURPAGE-Name.html"); when you want to send a file

结果

示例:

app.get("/",(req, res) => {
    res.sendFile(__dirname + "/Welcome.html");
});

欢迎.html

<!DOCTYPE>
<html>
    <head>
        <title>Data from MySQL</title>
    </head>
    <body>
        <p>Welcome to my page</p>
    </body>
</html>

2 - 使用 res.sendFile(__dirname + "/index.html"); 当你想要获取

2 - Use res.sendFile(__dirname + "/index.html"); when you want to get the

初始页面,以便您可以在要处理的应用程序中使用表单

initial page so then you can have forms within the application that you want to process

示例:

app.get("/",(req, res) => {
    res.sendFile(__dirname + "/index.html");
});

index.html

<!DOCTYPE>
<html>
    <head>
        <title>Data from MySQL</title>
    </head>
    <body>
        <form action="/" method="post">
          //YOUR FORM HERE 
        </form>
    </body>
</html>

这篇关于使用 Node.js 从 MySQL 中提取数据并显示在 HTML 页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆