繁体   English   中英

无法发布/登录错误 - 使用 Node.js 和 Express.js 使用 MySql 单击提交时

[英]Cannot POST /login error - while clicking submit using Node.js and Express.js with MySql

我正在尝试使用 node.js 和 express.js package 和 mysql package 构建简单的登录页面,如果用户名和密码存在于 mysql 数据库中,这会将用户重定向到 layout.html 页面。

我在本地主机上运行 mysql,并在 mysql 工作台中创建了一个包含表和用户的数据库。

node.js 服务器也使用 liteserver 在本地主机上运行。

在我点击页面上的“提交”按钮后,无论是否有任何数据写入(用户名)和(密码)框,我都会收到一条错误消息“无法发布/登录”

这就是我所有的代码,我所有的 javascript 代码都在一个文件中。

**脚本.js **


import express from 'express';
const app = express();
import { createConnection } from 'mysql';
import bodyParser from 'body-parser';

const router = express.Router();

// Create a connection to the MySQL server
const connection = createConnection({
  host: 'localhost', // The hostname of the MySQL server
  user: 'pablo', // The username to connect to the server
  password: 'KotWButach!', // The password for the usernam
  database: 'logowanie' // The name of the database
});

// Connect to the MySQL server
connection.connect();

connection.connect((err) => {
  if (err) {
    console.error(`Error connecting to the database: ${err.stack}`);
    return;
  }
  console.log(`Connected to the database with id: ${connection.threadId}`);
});

connection.query(query, (error, results) => {
  if (error) {
    console.error(`Error executing the query: ${error.stack}`);
    return res.status(500).send({ error });
  }
});



// Parse the request body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

router.post('/login');

app.post('/login', (req, res) => {
  const { username, password } = req.body; // Destructure the username and password from the request body

  // Query the users table to check if the provided username and password match a record in the table
  const query = `SELECT * FROM users WHERE UserName = '${username}' AND UserPass = '${password}'`;
  connection.query(query, (error, results) => {
    if (error) {
      return res.status(500).send({ error });
    }
    if (results.length === 0) {
      return res.status(401).send({ message: 'Invalid username or password' });
    }
    // If the username and password match a record in the table, redirect to the layout.html page
    res.redirect('/layout.html');
  });
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

index.html

<!DOCTYPE html>
<html>
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'nonce-randomvalue'">
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <title>Login</title>
  <script nonce="randomvalue" src="script.js"></script>
</head>
<body>
  <form action="/login" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username">
    <br>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password">
    <br><br>
    <input type="submit" value="Submit">
  </form> 
</body>
</html>

我希望它能够在单击提交后重定向到 layout.html(使用与数据库中的凭据匹配的凭据),或者在它们不匹配时收到错误提示“用户名或密码无效”。

我在 stackoverflow 上浏览了 30 多个案例,但似乎没有一个答案对我有用,我什至问过 ai 要做什么,但它卡在循环中告诉我检查我的路由是否正确。

您可以简单地删除代码中导致错误的这些行:

只删除这一行:

router.post('/login');

您声明了一个没有回调的 api 端点并指向“/login”,这就是您出现错误的原因。

删除后,您也可以删除之前声明的路由器,因为您不再需要它了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM