4 How do I use HTTP modules? -->http protocol
A introduces the HTTP module var http = require (' http ');
b Create server object var server = Http.createserver ();
C Listening Port Server.lisen (8080);
D Listen for requests event request
5 Setting the response header information---> Response type encoding
Request-to-Request header and request body
Responses--Response headers and response bodies
Address of the Remote address client
Method of submitting data by Request method
Content-type:text/html;charset=utf-8 the type of encoding and response set
Status code of the response:
200-Successful request
304-not modified. The requested resource is not modified and no resources are returned when the server returns this status code
404-The requested resource (Web page, etc.) does not exist
500-Internal Server error
The method above the response object:
Res.write ()
Res.end ()
Res.setheader ()
Res.writehead (Status code (200), ' corresponding displayed information (success) ', {incoming packet is here})
Send data to the server when requesting an object request
1 url:http (protocol)://localhost (domain name)/user?username=zhangsan&password
The domain name points to the root directory of the site
The data format for the content transfer after the question mark Key=value multiple Key/value key=value & key1=value1 (these data are called Query strings) &,
How to submit data:
1 Get mode: Submitted data displayed in the browser's address bar
2 post mode
How the server receives data
1 if it is a Get method submission
A URL module resolves the URL address to form an object var Objurl = Url.parse (Req.url);
b use QueryString to convert Objurl.query to Object Objurl.query (Key=value & key1=value1)
C Use the data in the form of an object. property
/** * Created by Administrator on 2016/10/31.*///1 Introducing HTTPvarHTTP = require (' http ');varFS = require (' FS ');varurl = require (' URL ');//This module is built specifically to parse URLs into an objectvarQueryString = Require (' querystring ')); convert to Object //2 creates a server object and listens for requestsvarServer = Http.createserver (function(req,res) {//The Req object has a URL attribute to get everything behind the domain name Req.ur This is a string //Console.log (req.url);Res.setheader (' Content-type ', ' text/html;charset=utf-8 '); //parsing URLs varObjurl =Url.parse (Req.url); //Console.log (objurl.pathname);Console.log (Objurl.query)//username=admin&pwd=123456 = = "{username:admin} if(Objurl.pathname = = = "/"){ //1 reading an HTML pageFs.readfile ('./get.html ', ' utf-8 ',function(err,data) {res.write (data); Res.end (); }) } if(Objurl.pathname = = = "/login"){ //receiving data from the browser side. //get to the username corresponding value to make judgment varQueryobj =Querystring.parse (objurl.query); Console.log (Queryobj); if(Queryobj.username = = = "Admin") {Res.write (' Login successful '); Res.end (); }Else{res.write (' Login failed '); Res.end (); } } if(Objurl.pathname = = = ' Home ') {Res.write (' Welcome to the personal Center '); Res.end (); }});//4 functions for binding monitoringServer.on (' Listening ',function() {Console.log (' Listening on 3000 ');});//3 Listening PortServer.listen (3000);
//1 Introducing HTTPvarHTTP = require (' http ');varMIME = require (' MIME ');//view the corresponding type of document that you are currently returningvarFS = require (' FS ');//2 Create a server object and listen for requestsvarServer = Http.createserver (function(req,res) {//Req Request Object //Res Response object in the response header to set the text type and encoding of the response //var txt = mime.lookup ('/path/to/file.mp4 '); = ' Text/plain ' //console.log (TXT); //mime.lookup (' file.txt '); = ' Text/plain ' //mime.lookup ('. TXT '); = ' Text/plain ' //mime.lookup (' htm '); = ' text/html ' //1 Use FS module to read this pageFs.readfile ('./get.html ', ' utf-8 ',function(err,data) {//tell the browser what type to use to parse the data I'm returning //Sometimes you don't know what the type of return should be, use the MIME module to see //res.setheader (' Content-type ', "text/html;charset=utf-8"); //Status Code response header set a state code different encoding means different meaning //200 indicatesres.write (data); Res.end (); })});//4 functions for binding monitoringServer.on (' Listening ',function() {Console.log (' Listening on 3000 ');});
FS Path URL QueryString http MIME
HTTP Module
1 Creating a server
A introduces HTTP var http = require (' http ')
b create var server = Http.createserver (function (req,res) {
Business logic
})
C Listening Port Server.listen (8080)
2 Request and Response objects
Request.url Get the URL address
Res.setheader (Key,value) Setting the response header information
Res.writehead (' Status code ', ' description information ', {})
Res.write ()
Res.end ()
3 browsers and servers communicate with each other
The browser sends data to the server:
A is sent by get: Inside the form there is get way a label a href= "Localhost:8080/user?username=xxx"
B Post mode to send:
How the server receives the browser's data:
The data sent by a Get mode:
1 First take the URL to 2 to parse the URL into object var 0bj = Url.parse (Req.url) s
3 Dataobj = Querystring.parse (obj.query)
B post-mode data:
1 req.on (' Data ', function (data) {}) 2 Req.on (' End ', function () {}) 3 querystring If it is a post submission
The server sends data to the browser:
Res.write ();
Res.end ();
HTTP module, get