How to use the server to achieve HTML page data submission? We can do this using GET and POST requests, respectively

First of all:

Here is a simple HTML page:

<! DOCTYPEhtml>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<script type="text/javascript"></script>
</head>
<body>
<form action="http://127.0.0.1:3000" method="post">
	<div>
		<label for="username">The user name</label>
		<input type="text" name="username" id="username" placeholder="Please enter user name">
	</div>
	<div>
		<label for="password">password</label>
		<input type="password" name="password" id="password" placeholder="Please enter your password">
	</div>
	<div>
		<button type="submit">submit</button>
	</div>
</form>
</body>
</html>
Copy the code

Note:

The input tag in the form must have a name attribute, otherwise the data entered by the user will not be sent to the server when the form is submitted.

The first is the implementation of the GET request:

//1. Use the HTTP module first. When using the variable name, we usually use the name of the module.
const http = require('http')
//2. Cite url module, its function is mainly parse URL string and URL object
const url = require('url')
//3. Next, create the service
let server = http.createServer(function (req,res){
    //3.1 Handle Chinese garbled characters
    req.setEncoding("utf-8")
    res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'})
    If the result is the same as the result twice, add the following judgment to the if statement
    if(req.url ! = ='/favicon.ico') {//3.3 Url.parse is used to convert the URL string to an object format. This method takes two arguments. The first argument is the URL string, and the second argument is a Boolean value, which can be left blank, indicating whether to convert query to an object
        let query = url.parse(req.url,true).query
        //3.4 Next we can test the user name and password given
        if (query.username === "admin"&&query.password === "123456"){
            res.write("

Login successful

"
)}else { res.write("

Login failed

"
) } } res.end() }) //4. Node service listener server.listen(3000.function (){ console.log("running......")})Copy the code

The second is POST request implementation:

When we change to post request to submit the form, we will find that when you click submit, the address bar will not show your username and password, so we need a module to handle the parameters of post request.

//1. The same first step is to introduce the HTTP module
const http = require('http')
//2. Introduce the queryString module to format the parameters of the POST request
const queryString = require('querystring')
//3. Create services
http.createServer(function (req,res){
    //3.1 Handle the problem of Chinese garble
    req.setEncoding("utf-8")
    res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'})
    If the result is the same as the result twice, add the following judgment to the if statement
    if(req.url ! = ='/favicon.ico') {// Initialize the POST variable to hold information about the POST request
        let post = ' '
        // THE POST request listens for the data event to get the content
        req.on('data'.function (data){
            post += data
        })
        // Listen for the end event, which indicates that the POST request parameters have been superimposed
        req.on('end'.function (){
            // Handle the POST variable
            let query = queryString.parse(post)
            res.write('

POST request processing

'
) if (query.username === "admin"&&query.password === "123456"){ res.write("

Login successful

"
)}else { res.write("

Login failed

"
) } res.end() }) } }).listen(3000.function (){ console.log('running..... ')})Copy the code