1.1 Request and Response
Request and response model
Developer tools left bar request, right bar response
How to send a request
-
Methods:
Use the Chrome address bar
Use the curl command
-
concept
The tool that helps send requests is called a User Agent.
How to make a response
-
Need to programming
Node.js has an HTTP module that does this
VSCode creates a terminal and runs Node server.js 8888
Curl the path under this port in cmder. VSCode will refresh the terminal
-
Pay attention to
This code is the server code and is generally placed on the server.
Path is the path /x without query parameters
Query is the object form of the query parameter {a:’1′}
QueryString is a string form of a query parameter, okay? a=1
PathWithQuery is a pathWithQuery parameters
Request is the request object
Response is the response object
Code logic
-
grammar
-
logic
The code is executed every time a request is received.
Use if else to determine the path, return different content.
If it’s a known path, always return 200.
If the path is unknown, 404 is returned.
Content-type indicates the “Type/syntax” of the Content
Response.write () can fill in the returned content
Response.end () indicates that the response can be sent to the user
conclusion
-
Server program – example above
-
Web page –
/ path returns an HTML content
The /x path returns one CSS content
The /y path returns a JS content
-
Note –
The suffix in the URL is useless. /y.css is not necessarily CSS content
The content-type is what determines the file Type
1.2 HTTP
Systematic learning
-
Basic concepts – request, response
-
How to debug – with Node.js, you can use log/debugger
-
Resources – Use Node.js and look at the Node.js documentation
-
Standard – HTTP specification documents: RFC2612, etc
HTTP Basic Concepts
request
Request verb Path + Query parameters Protocol name/version Host: domain name or IPAccept: text/ htmlContent-type: format of the request body Enter the request body (upload content)Copy the code
Three parts: request line, request header, request body
Request verbs GET/POST/PUT/PATCH/DELETE, etc
The request body is generally empty in a GET request
Case insensitive
RFC 2612 chapter 5
- The GET demo –
- The POST –
The response
Protocol name/version status code Status string Content-Type: response body format Enter Response body (download Content)Copy the code
Three parts: status line, response header, response body
Common status codes:
Section 6 of RFC 2612
- Demo –
Response body –
Construct the request with curl
When the server is enabled
The curl -v http://127.0.0.1:8888Copy the code
Set the request verb
-X POST
Copy the code
Set paths and query parameters
Add it directly after the URL
Set the request header
-H 'Name:Value'
Copy the code
or
--header 'Name:Value'
Copy the code
Set the request body
-d 'contents'Copy the code
or
--data 'contents'Copy the code
Read the request with Node.js
Read request verb
request.method
Curl sends GET and POST requests
GET the request verbs GET and POST in the terminal, respectively
Read the path
Request. url path with query parameters
Path Indicates a pure path without query parameters
Query Has only query parameters
Read request header
request.headers['Accept']
Read request body
Set the response with Node.js
Set the response status code
response.statusCode = 200
Example Change the status code 200 to 201
Using curl, you can obtain the status code 201
Setting the response header
response.setHeader('Content-Type','text/html');
Set the response body
Response. Write (' content ')
Append content:
The server
Server Setup Procedure
-
Buy a server in Aliyun
-
Copy the local public key to ali cloud ~/.ssh/authorized_keys
Get the local public key first
Echo 'Local public key' >> ~/. SSH /authorized_keysCopy the code
-
Remotely operate the cloud machine locally using SSH root@ instance IP
Access files in the root directory of the server
-
You can name the instance IP in hosts
Notepad opens hosts as an administrator and names the instance IP address aliyun1
-
To exit the cloud machine, type Exit.
How do I prevent SSH jams
Method 1 –
Add to /etc/ssh/ssh_config at the end
Host * ServerAliveInterval 30
Copy the code
Method 2 –
You can do this twice with Echo (locally, not in the cloud)
echo "Host *" >> /etc/ssh/ssh_configecho " ServerAliveInterval 30" >> /etc/ssh/ssh_config
Copy the code
Creating an Application Account
The Linux root account has the highest permissions to prevent attacks, so create a new application account.
Steps:
-
adduser river
-
Enter new UNIX password: Enter the password
-
Enter your password again
-
Press Enter all the way, then run the command —
mkdir /home/river/.sshcp ~/.ssh/authorized_keys /home/river/.ssh/chmod 755 /home/river/.ssh/authorized_keyschown river:river /home/river/.ssh/authorized_keys Copy the code
-
Finally, SSH river@aliyun1 can be implemented
sudo
Add sudo permission to river (root)
adduser river sudo
Copy the code
What is sudo?
Super User DO, similar to “Run as administrator” on Windows
When using the river account, if you encounter special operations before using sudo, please exit root
You need to enter the password of the river user
sudo !! Execute the preceding command with sudo
Installation Node. Js 14
steps
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -sudo apt-get updatesudo apt-get install -y nodejsnode -vnpm -vnpx -v
Copy the code
Install git
steps
sudo apt install git
Copy the code
Git -m “XXX”
Learn vim
The deployment of application
1. The cloning nodejs – test
clone https://github.com/RiverCui/nodejs-test.git
Copy the code
2. Create a log in the nodejs-test file
cd nodejs-testtouch log
Copy the code
3. Start the command
node server.js 8888 > log 2>&1 &
Copy the code
This process is already running in the background with id 19405, local port of curl remote machine:
To shut down the process, run the following command
kill -9 id
Copy the code
4. Make the start command into a start file
touch startecho 'node server.js 8888 > log 2>&1 &'chmod +x ./start
Copy the code
Now you can run start directly
. / start or sh. / startCopy the code
Since start is run through Node, it can pass at this point
killall node
Copy the code
Stop all Node processes
After starting the command, you can access the page using Ali Cloud IP+ port number
How to Restart an Application
SSH frank@ instance ipcd nodejs-testCopy the code
Pull the latest code
git pull
Copy the code
Start the
killall nodesh ./start
Copy the code