nodejs registration jumps to login html

WBOY
Release: 2023-05-27 20:16:07
Original
745 people have browsed it

Node.js is an event-driven I/O server framework developed based on JavaScript language. Its emergence has brought revolutionary changes to Internet application development. In Node.js, you can use the HTTP module to create a web server and send HTML pages, CSS, JavaScript and other resources to the client.

This article describes how to implement the function of jumping to the login page after user registration in Node.js. The specific code implementation is as follows:

First, create a Web application on the server side, using the HTTP module and Express framework:

var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
Copy after login

Then, add a routing handler in the application to handle registration separately And the request for the login page:

//注册页面
app.get('/register', function(req, res) {
    res.sendFile(__dirname + "/register.html");
});

//登录页面
app.get('/login', function(req, res) {
    res.sendFile(__dirname + "/login.html");
});
Copy after login

Among them, __dirname is a global variable in Node.js, indicating the absolute path of the directory where the current module is located.

Next, use the body-parser module to obtain the data of the POST request:

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
Copy after login

Then, add a form to the registration page and send it to /registerRoute sends POST request:

<form action="/register" method="post">
    <label>用户名:</label>
    <input type="text" name="username"><br>
    <label>密码:</label>
    <input type="password" name="password"><br>
    <input type="submit" value="注册">
</form>
Copy after login

On the server side, add the code to handle the POST request of /register route:

app.post('/register', function(req, res) {
    var username = req.body.username;
    var password = req.body.password;
    //用户注册代码
    res.redirect('/login');
});
Copy after login

This route handler will get the POST request data, register a new user, and redirect to the login page.

Finally, add a form to the login page and send a POST request to the /login route:

<form action="/login" method="post">
    <label>用户名:</label>
    <input type="text" name="username"><br>
    <label>密码:</label>
    <input type="password" name="password"><br>
    <input type="submit" value="登录">
</form>
Copy after login

On the server side, add processing /loginRouting POST request code:

app.post('/login', function(req, res) {
    var username = req.body.username;
    var password = req.body.password;
    //用户登录验证代码
    res.send("登录成功");
});
Copy after login

The routing handler will obtain the data of the POST request, verify the user's login information, and return a successful login prompt.

The above is all the code for Node.js to implement user registration and jump to the login page. The implementation method is simple and direct, suitable for beginners to refer to.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template