Home >Web Front-end >Front-end Q&A >How to implement login page jump in Node.js application

How to implement login page jump in Node.js application

PHPz
PHPzOriginal
2023-04-05 13:47:51861browse

Node.js (Node for short) is a JavaScript runtime environment based on the V8 engine, which allows JavaScript to run on the server side. Using Node.js for web development has the advantages of being lightweight, efficient, and scalable.

This article will introduce how to implement login page jump in Node.js application.

  1. Create login page HTML file

First, we need to create a login page HTML file, which should include a form for users to enter their username and password. , and a submit button for submitting the form. The HTML code is as follows:

nbsp;html>


    <meta>
    <title>Login</title>


    <h1>Login</h1>
    
        
                                  
        
                                  
             

In the form, we define method as post, which means using POST method to submit the form data, in action The login address /login is specified.

  1. Complete the login processing logic of the Node.js application

When the user submits the form, the Node.js application will receive the POST request and process the username and password entered by the user. password. In this example, we use a simple way to store the username and password directly in variables on the server side, and jump to the homepage after verifying that the login is successful (in this example we use /home as the homepage address).

The code is implemented as follows (using the Express framework):

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

// 定义用户名和密码
const username = 'admin';
const password = '123456';

// 处理登录请求
app.post('/login', function (req, res) {
    const { username: inputUsername, password: inputPassword } = req.body;
    if (inputUsername === username && inputPassword === password) {
        res.redirect('/home');
    } else {
        res.send('Invalid username or password');
    }
});

// How to implement login page jump in Node.js application
app.get('/home', function (req, res) {
    res.send('Welcome to the home page');
});

// 启动服务
app.listen(3000, function () {
    console.log('Server started on port 3000');
});

In the above code, we use the body-parser middleware to parse the JSON sent by the client POST request data and store the username and password in inputUsername and inputPassword. We then compare it with the preset username and password, and if the match is successful, use res.redirect to jump to the homepage/home , otherwise an error message will be sent.

Note that we simply sent a welcome message in the /home route. In actual development, we need to specify the appropriate view template to render and display the page.

  1. Run the application

Now, we start the Node.js application and visit the login page in the browserhttp://localhost:3000 , you can see the login form (if there is no exception in the project), as shown below:

How to implement login page jump in Node.js application

Enter the correct user name and password, click the "Login" button, and the page will jump Go to the home page /home, as shown below:

How to implement login page jump in Node.js application

So far, we have implemented the login page jump function in the Node.js application. You The code can also be further improved to support more login methods and user verification methods.

The above is the detailed content of How to implement login page jump in Node.js application. 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