gpt4 book ai didi

javascript - 随叫随到执行 MySQL 操作 - Node.js

转载 作者:行者123 更新时间:2023-11-29 16:46:09 25 4
gpt4 key购买 nike

我一直在学习一些 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数据库?

最佳答案

据我在您的代码中看到的,您正在使用表单传递的数据在用户表中设置 INSERT,并将服务器设置为在操作完成之前进行响应,以便页面无论如何都会收到 awnser,但是 awnsering 您的问题,是的,您在 Node 服务器的“index.js”中放入的操作一旦启动就会运行。

关于javascript - 随叫随到执行 MySQL 操作 - Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53109313/

25 4 0
文章推荐: javascript - 几秒钟后停止 requestAnimationFrame
文章推荐: mysql - 为什么 "explain"返回的行不等于 count()?
文章推荐: mysql - Joomla 3x - 配置文件字段
文章推荐: javascript - 如何将 SVG 附加到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com