node.js写登录注册接口
时间: 2023-08-10 10:12:00 浏览: 59
好的,下面是一个简单的使用 node.js 和 express 框架编写的登录注册接口示例:
```javascript
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const app = express();
app.use(bodyParser.json());
// 模拟数据库中的用户数据
const users = [
{
id: 1,
name: 'Alice',
email: 'alice@example.com',
password: '$2a$10$N8l4ZvMwzG8dD0.SbVLU6OT7S8.PQPGJYiKkkLZ5zR5nHZQ4q9n4W', // 密码为 "password"
},
{
id: 2,
name: 'Bob',
email: 'bob@example.com',
password: '$2a$10$N8l4ZvMwzG8dD0.SbVLU6OT7S8.PQPGJYiKkkLZ5zR5nHZQ4q9n4W', // 密码为 "password"
},
];
// 注册接口
app.post('/register', async (req, res) => {
try {
// 从请求体中获取用户名、邮箱和密码
const { name, email, password } = req.body;
// 检查用户名和邮箱是否已经被注册
const existingUser = users.find(user => user.name === name || user.email === email);
if (existingUser) {
return res.status(400).json({ message: 'User already exists' });
}
// 对密码进行哈希处理
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
// 创建新用户
const newUser = {
id: users.length + 1,
name,
email,
password: hashedPassword,
};
users.push(newUser);
res.status(201).json({ message: 'User created' });
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// 登录接口
app.post('/login', async (req, res) => {
try {
// 从请求体中获取邮箱和密码
const { email, password } = req.body;
// 查找用户
const user = users.find(user => user.email === email);
if (!user) {
return res.status(400).json({ message: 'Invalid credentials' });
}
// 验证密码
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
return res.status(400).json({ message: 'Invalid credentials' });
}
res.json({ message: 'Login successful' });
} catch (err) {
res.status(500).json({ message: err.message });
}
});
// 启动服务器
app.listen(3000, () => console.log('Server started'));
```
在上面的示例中,我们使用了 `bcrypt` 库对用户密码进行哈希处理,并使用 `express` 框架编写了两个接口:`/register` 和 `/login`。其中,`/register` 接口用于注册新用户,`/login` 接口用于用户登录验证。接口返回的是 JSON 格式的数据。如果请求出现错误,接口会返回相应的错误信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)