帮我用nodejs写带注释的登录
时间: 2023-05-13 09:04:59 浏览: 50
Sure, I can help you with that! Here's a sample code for a login system using Node.js:
```
// Import required modules
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
// Create an instance of the express app
const app = express();
// Set up middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: 'mysecretkey',
resave: true,
saveUninitialized: true
}));
// Define a route for the login page
app.get('/login', (req, res) => {
res.send(`
<form method="POST" action="/login">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<button type="submit">Login</button>
</form>
`);
});
// Define a route for handling the login form submission
app.post('/login', (req, res) => {
const { username, password } = req.body;
// Check if the username and password are correct
if (username === 'admin' && password === 'password') {
// Set the user's session
req.session.user = { username };
res.redirect('/dashboard');
} else {
res.send('Invalid username or password');
}
});
// Define a route for the dashboard page
app.get('/dashboard', (req, res) => {
// Check if the user is logged in
if (req.session.user) {
res.send(`Welcome, ${req.session.user.username}!`);
} else {
res.redirect('/login');
}
});
// Start the server
app.listen(3000, () => {
console.log('Server started on port 3000');
});
```
This code sets up an Express app with middleware for parsing form data and managing sessions. It defines routes for the login page and dashboard page, and handles the login form submission by checking the username and password and setting the user's session if they are correct. The dashboard page checks if the user is logged in and displays a welcome message if they are, or redirects to the login page if they are not.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)