我试图使一个非常简单的登录网页,没有安全或类似的东西。我的系统现在由一个OpenSuse服务器、一个mariadb数据库、一个Express服务器和一个用于客户端的HTML文件组成。
Express服务器:
const mariadb = require('mariadb');
const express = require('express');
const session = require("express-session");
const http = require('http');
const app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
const expresServer = http.createServer(app);
var connection = mariadb.createPool({
host: "localhost",
user: "user",
password: "pass",
database: "users",
connectionLimit: 2
});
app.use(express.static(__dirname + '/client'))
app.get("/", (req, res) => {
res.sendFile(__dirname + '/client/login.html')
})
app.post('/auth', function(request, response) {
// Capture the input fields
let username = request.body.username;
let password = request.body.password;
// Ensure the input fields exists and are not empty
if (username && password) {
// Execute SQL query that'll select the account from the database based on the specified username and password
connection.query('SELECT * FROM USERS WHERE User = ? AND Pass = ?', [username, password], function(error, results, fields) {
// If there is an issue with the query, output the error
if (error) throw error;
// If the account exists
if (results.length > 0) {
// Authenticate the user
request.session.loggedin = true;
request.session.username = username;
// Redirect to home page
response.redirect('/main');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
app.get('/main', function(request, response) {
// If the user is loggedin
if (request.session.loggedin) {
// Output username
response.send('Welcome back, ' + request.session.username + '!');
} else {
// Not logged in
response.send('Please login to view this page!');
}
response.end();
});
expresServer.listen(3000, () => {
console.log("Listening on 3000");
})
HTML登录:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>UAV5G</title>
<link rel="shortcut icon" href="/imgs/Logo.png" />
<link rel="stylesheet" href="css/login.css" media="screen" />
<link rel="stylesheet" href="css/all.min.css" />
</head>
<body>
<div class="elem-box">
<div class="login-box">
<h2>Login</h2>
<form action="/auth" method="post">
<div class="user-box">
<input type="text" name="username" id="username" required>
<label for="username">Username</label>
</div>
<div class="user-box">
<input type="password" name="password" id="password" required>
<label for="password">Password</label>
</div>
<input class="login" type="submit" value="Login">
</form>
</div>
<img src="/imgs/Logo.png" class="logo">
</div>
</body>
</html>
我不认为CSS是必要的。
这里的问题是Express服务器抛出以下错误:
/home/node/Server/node_modules/mariadb/lib/misc/errors.js:61
return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
^
SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10010ms
(pool connections: active=0 idle=0 limit=2)
at module.exports.createError (/home/node/Server/node_modules/mariadb/lib/misc/errors.js:61:10)
at Pool._requestTimeoutHandler (/home/node/Server/node_modules/mariadb/lib/pool.js:344:26)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
sqlMessage: 'retrieve connection from pool timeout after 10010ms\n' +
' (pool connections: active=0 idle=0 limit=2)',
sql: null,
fatal: false,
errno: 45028,
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
}
我不知道为什么,我可以使用该用户名和密码从控制台连接到数据库,并添加connectTimeout:10000(或更高)没有帮助。
2条答案
按热度按时间zujrkrfu1#
使用mariadb连接器时出错:有两种不同的实现方式:Promise和Callback
const mariadb = require('mariadb');
const mariadb = require('mariadb/callback');
这里的问题是,你使用promise实现,然后调用回调方法:
因此,要么将其更改为Promise,要么使用回调实现
kyks70gy2#
我想你必须在得到你的数据后释放连接。可能会给你带来麻烦。