My first web server

// Create a web server 


// reference system module 
const = HTTP The require ( 'HTTP') // for creating web server module 
// create web server 
const = http.createServer App (); // App is the site server object 
/ / when a client sends a request 
app.on ( 'request', (REQ, RES) => {
     // Get request output mode 
    console.log (req.method);
     // Get message information output request 
    console.log ( req.headers);
     // Get address output request 
    the console.log (req.url) 


    res.end ( '<h1 of> Hi User </ h1 of>' )
     // // res.writeHead (HTTP status code, { 
    //      'content-type': 'content type'text/plain
    // })
    @ Content Type: text / Plain 
                // text / HTML 
                // text / CSS etc. 
    res.writeHead (200 is , {
         'Content-type': 'text / HTML: UTF8 charset =' 
    }) 
}) 
// listener 3000 port 
app.listen (3000 ); 
console.log ( 'server has been started, listening 3000 port, please visit localhost: 3000' ); 


// HTTP protocol hypertext transfer protocol 
// concept: client and server communication norms 
// packets: request packets 
    //    response message: 
    // 200 requests a successful HTTP status code 404 requests a resource not found 500 server error 400 client requests a syntax error 
// request method: 1.get request data 
//           2 .post send data 
   


//   parameter after the gET request parameters are placed in the browser address bar? available through req.url
// a GET request parameters: 
const the require url = ( 'url') // for processing the url short generally used for login information 
// Prase at url () method 
url.parse (req.url) // after parsing the object returned contains many properties, such as query contains the address parameters to be analyzed in the form of pathname representation? Before the url address 
url.parse (req.url, to true ) // The first parameter is to resolve the address of the second parameter is a Boolean value represents whether the query parameters parsed into objects form 
url.parse (req.url, to true ) .query // object parsed address? After the parameters are stored in the attribute property values 
// the let url.parse the params = (req.url, to true) .query 
// the params. Property parameters representative analytical 
// example = ... params.age 



// the POST request parameters in the request packet
// reference system module 
const = HTTP The require ( 'HTTP') // module to create a site for the server 
// create web server 
const = http.createServer App (); // App is the site server object 
const querystring = require ( ' QueryString ') // processing request parameter module 
// when a client sends a request 
app.on (' request ', (REQ, RES) => {
     // receiving post is received parameter data parameter post events and end events through the event scheme disposable and may not have been received 
    //    trigger data when the parameter data delivery request event 
    //    end end triggering event parameter transfer is complete when 
  the let postParams = '' ; 
    req.on ( 'data', the params => { 
    postParams + =   the params; 
    }) 

    req.on ('End', () => { 
 the console.log (postParams); // output request parameter 
 the console.log (querystring.parse (postParams)); // outputs the converted request format of the output parameters as objects 
 
 
    }) 
    RES. end ( 'OK') // server sends a response indicates that the received request response parameter waiting for a client to terminate 
})
 // listening port 3000 
app.listen (3000 ); 
the console.log ( 'server, listening port 3000, visit localhost : 3000 ');
 
 

 

 

 

Guess you like

Origin www.cnblogs.com/treasurea/p/11246675.html