I am trying to make a very simple login webpage, no security or anything like that. My system right now consists of an OpenSuse server with a mariadb database and an Express server and an HTML file for the client.
Express server:
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 login:
<!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>
I do not think CSS is needed.
The problem here is that the Express server throws this error:
/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 10010msn' +
' (pool connections: active=0 idle=0 limit=2)',
sql: null,
fatal: false,
errno: 45028,
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
}
I do not know why, I can connect to the database from console using that username and password and adding connectTimeout: 10000 (or higher) does not help.
2
Answers
I think you must have to release the connection after you get your data. Might be causing all the troubles for you.
There is an error using mariadb connector:
there is 2 different implementation : promise and callback.
const mariadb = require('mariadb');
const mariadb = require('mariadb/callback');
problem here is that you use promise implementation then call a callback method:
so either you change that to promise, or use callback implementation