【问题标题】:Do MySQL actions on call - Node.js随时执行 MySQL 操作 - Node.js
【发布时间】:2019-04-06 03:38:24
【问题描述】:

我一直在学习一些 Node.js,并试图制作一个程序,您可以在其中输入用户名和密码,并根据 MySQL 数据库对其进行检查。我不知道我是否正确地进行了整个身份验证业务,但我的问题是:你能在代码开始后调用 MySQL 函数吗(即在某种函数调用上)。

您可以对函数调用执行 MySQL 操作吗?

我查看了互联网和不同的 Stack Overflow 问题,但我仍然不太明白。不过,我可能错过了关于 Node.js 实际作用的技巧。

这是我的代码:

HTML:

<html>
    <head>
        <title>Basic User Information</title>
    </head>
    <body>
        <form action="http://localhost:8888/user" method="POST">
            Username: <input type="text" name="username"> <br>
            Password: <input type="text" name="password"> <br></select> 
            <input type="submit" value="submit">        
        </form>
    </body>
</html>

Node.js:

//import the express module
var express = require('express');

//import body-parser
var bodyParser = require('body-parser');

//store the express in a variable 
var app = express();

var mysql = require('mysql');

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

con.connect(function(err) {
    if (err) throw err;
    console.log("Connected!");
    con.query("CREATE DATABASE authtest", function (err, result) {
        if (err) throw err;
        console.log("Database created");
    });

    con.query("CREATE TABLE users (username VARCHAR(255), password VARCHAR(255))", function (err, result) {
        if (err) throw err;
        console.log("Table created");
    });
});

//configure body-parser for express
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

//allow express to access our html (index.html) file
app.get('/index.html', function(req, res) {
    res.sendFile(__dirname + "/" + "index.html");
});

//route the GET request to the specified path, "/user". 
//This sends the user information to the path  
app.post('/user', function(req, res){
    response = {
        username : req.body.username,
        password : req.body.password
    };

    //this line is optional and will print the response on the command prompt
    //It's useful so that we know what information is being transferred 
    //using the server
    console.log(response);

    //convert the response in JSON format
    res.end(JSON.stringify(response));

    con.connect(function(err) {
        if (err) throw err;
        var sql = "INSERT INTO users (username, password) VALUES (response.username, response.password)";
        con.query(sql, function (err, result) {
            if (err) throw err;
            console.log("1 record inserted");
        });
    });
});

//This piece of code creates the server  
//and listens to the request at port 8888
//we are also generating a message once the 
//server is created
var server = app.listen(8888, function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("Example app listening at http://%s:%s", host, port);
});

编辑:

我需要在另一个脚本中执行此操作吗?那么,有一个用于初始化页面,一个用于将数据插入到 MySQL 数据库中?

【问题讨论】:

  • 您能否进一步解释一下“代码开始后”或“函数调用上的 MySQL 操作”是什么意思?至少我不太明白你的问题是什么
  • @SamiKuhmonen 在代码底部附近,res.end(JSON.stringify(response)); 之后,我试图将用户输入从输入框中插入到 MySQL 数据库中,但我不知道如何。

标签: javascript html mysql node.js express


【解决方案1】:

据我所知,在您的代码中,您正在使用表单传递的数据在 users 表中设置一个 INSERT,并设置服务器在操作完成之前响应,以便页面无论如何都会接收 awnser,但要为您的 awnser问题,是的,您在节点服务器的“index.js”中放置的操作会在它启动后立即运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2011-11-21
    • 1970-01-01
    • 2017-05-21
    • 2012-01-14
    • 2022-06-17
    相关资源
    最近更新 更多