用node.js,mysql写一个登录注册
时间: 2023-06-11 20:06:06 浏览: 65
好的,下面是一个使用 Node.js 和 MySQL 实现的登录注册示例。
首先,需要安装以下两个 npm 包:
- `mysql`: 用于连接和操作 MySQL 数据库。
- `express`: 用于创建 Web 应用程序和 API。
在安装这两个包之前,需要确保已经安装了 Node.js 和 MySQL。
安装 `mysql` 和 `express`:
```
npm install mysql express
```
接下来,创建一个 `server.js` 文件,并在其中添加以下代码:
```javascript
const express = require('express');
const mysql = require('mysql');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// 配置 MySQL 数据库连接
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password', // 修改为你的 MySQL 密码
database: 'mydb' // 修改为你的数据库名
});
// 连接 MySQL 数据库
connection.connect((err) => {
if (err) {
console.error('error connecting to MySQL database: ' + err.stack);
return;
}
console.log('connected to MySQL database as id ' + connection.threadId);
});
// 使用 body-parser 中间件解析 POST 请求体
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// 注册 API
app.post('/register', (req, res) => {
const { username, password } = req.body;
connection.query(`SELECT * FROM users WHERE username = '${username}'`, (err, result) => {
if (err) {
console.error('error querying MySQL database: ' + err.stack);
res.status(500).send('Internal Server Error');
return;
}
if (result.length > 0) {
res.status(409).send('Username already exists');
return;
}
connection.query(`INSERT INTO users (username, password) VALUES ('${username}', '${password}')`, (err) => {
if (err) {
console.error('error querying MySQL database: ' + err.stack);
res.status(500).send('Internal Server Error');
return;
}
res.status(201).send('User created successfully');
});
});
});
// 登录 API
app.post('/login', (req, res) => {
const { username, password } = req.body;
connection.query(`SELECT * FROM users WHERE username = '${username}' AND password = '${password}'`, (err, result) => {
if (err) {
console.error('error querying MySQL database: ' + err.stack);
res.status(500).send('Internal Server Error');
return;
}
if (result.length === 0) {
res.status(401).send('Invalid username or password');
return;
}
res.status(200).send('Login successful');
});
});
// 启动服务器
app.listen(port, () => {
console.log(`server listening at http://localhost:${port}`);
});
```
以上代码实现了两个 API:
- `/register`: 注册 API,用于创建用户账号。需要传入 `username` 和 `password` 参数。
- `/login`: 登录 API,用于验证用户账号和密码是否正确。需要传入 `username` 和 `password` 参数。
在上面的代码中,首先使用 `mysql` 包连接 MySQL 数据库。然后,使用 `express` 包创建一个 Web 服务器,并定义两个 API。
在 `/register` API 中,首先查询数据库中是否存在相同的用户名。如果存在,则返回 `Username already exists` 错误响应。如果不存在,则在数据库中插入新用户记录,并返回 `User created successfully` 成功响应。
在 `/login` API 中,首先查询数据库中是否存在给定的用户名和密码。如果不存在,则返回 `Invalid username or password` 错误响应。如果存在,则返回 `Login successful` 成功响应。
最后,启动服务器并监听端口 3000。
使用 MySQL Workbench 或命令行工具创建一个名为 `mydb` 的新数据库,并创建一个名为 `users` 的新表,该表包含 `username` 和 `password` 两个列。在 `users` 表中插入一些测试数据,以便在注册和登录时使用。
运行 `node server.js` 启动服务器,然后使用 Postman 或其他 API 测试工具测试 `/register` 和 `/login` API。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)