Home  >  Article  >  Web Front-end  >  nodejs login page jump page jump page jump

nodejs login page jump page jump page jump

WBOY
WBOYOriginal
2023-05-27 21:00:531683browse

With the rapid development of Internet technology, the number of users of websites and applications is also increasing. In order to better manage and maintain applications, many developers choose to use Node.js as the back-end technology. In Node.js, the login page is one of the important parts of the application. In this article, we will explore how to implement login pages and page jumps using Node.js.

1. Create a login page
In Node.js, we can easily create a login page through the HTTP module and Express framework. First, you need to install the Express module in the project root directory.

npm install express --save

Create a file named index.js and introduce the express module into it. Based on this, create an express instance and specify the port number.

const express = require('express');
const app = express();

app.listen(3000, () => {
  console.log('应用已启动,端口号为3000');
});

Next, we need to create a route to handle the login request and display the login page.

app.get('/', (req, res) => {
  res.send(`
    <h1>登录页面</h1>
    <form method="POST" action="/login">
      <input type="text" placeholder="用户名" /><br />
      <input type="password" placeholder="密码" /><br />
      <button type="submit">登录</button>
    </form>
  `);
});

app.post('/login', (req, res) => {
  // 处理登录请求
});

In the above code, we use the app.get method to create a root route for displaying the login page. At the same time, the /login route is created using the app.post method to handle login requests.

2. Processing login requests
In the previous section, we created a /login route to handle login requests. In this route, we can obtain the username and password entered by the user in the login form through the req object, and then verify it.

For example, we can hard-code the username and password in the Node.js program for verification. The code is as follows:

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

  if (username === 'admin' && password === '123456') {
    res.send('登录成功!');
  } else {
    res.send('用户名或密码不正确');
  }
});

In the above code, we use req.body to obtain the login form username and password. Then it is verified based on the user name and password. If the verification is passed, a prompt message of successful login is returned, otherwise a prompt message of failed login is returned.

3. Page jump
After successful login, we usually jump the user to other pages. In Node.js, we can use the res.redirect method to implement page jumps and point the response results to other routing addresses.

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

  if (username === 'admin' && password === '123456') {
    res.redirect('/home');
  } else {
    res.send('用户名或密码不正确');
  }
});

app.get('/home', (req, res) => {
  res.send(`
    <h1>首页</h1>
    <p>欢迎访问首页</p>
  `);
});

In the above code, after successful login, use res.redirect to point the response result to the /home route, which is the home page. In the /home route, we can display the content of the home page to the user.

4. Use Session to implement user authentication
In Node.js applications, we generally use Session to implement user authentication. After successful login, the user information is stored in the Session, and the contents of the Session need to be verified when accessing other pages.

The specific implementation steps are as follows:

1. Install the express-session module in the project root directory.

npm install express-session --save

2. Use the express-session module to set Session related parameters.

const session = require('express-session');

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: false }
}));

In the above code, we use the app.use method to use the express-session middleware to set the Session secret parameters, resave parameters, saveUninitialized parameters and cookie parameters.

3. When the login is successful, store the user information in the Session.

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

  if (username === 'admin' && password === '123456') {
    req.session.user = {username};
    res.redirect('/home');
  } else {
    res.send('用户名或密码不正确');
  }
});

In the above code, we use req.session.user to store the information of the currently logged in user into the Session.

4. Perform Session verification when accessing other pages.

app.get('/home', (req, res) => {
  if (req.session.user) {
    res.send(`
      <h1>首页</h1>
      <p>欢迎访问首页,${req.session.user.username}</p>
    `);
  } else {
    res.redirect('/');
  }
});

In the above code, we first use the if statement to determine whether user information exists in the Session. If it exists, display the content of the homepage and display the username of the currently logged in user to the user. If it does not exist, redirect to the login page.

Summary
In Node.js, creating login pages and page jumps are important aspects of application development. Through the introduction of this article, we have learned how to use Node.js and Express framework to create login pages, handle login requests and page jumps. In addition, we also learned how to use Session to implement user authentication. In actual applications, developers need to make corresponding adjustments and optimizations according to specific application scenarios to meet user needs.

The above is the detailed content of nodejs login page jump page jump page jump. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn