Home > Article > Web Front-end > How to implement login page jump in Node.js application
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.
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.
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.
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:
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:
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!