Achieve a static server with Node.js native code

--- --- restore content begins

There are two types of back-end server
1. web server server [Static]
- Example: wamp inside the www directory
- The purpose is to show the page content
- Front End: nginx
2. Application Servers [api server]
- Backend Interface
- tomcat

doing what?
- Use native code to achieve static Node.js server [will be]
the require HTTP = const ( 'HTTP' ) 

const Port = 3000 

const hostname = 'localhost' // 127.0.0.1 

http.createServer ((Request, Response) => { 

response.writeHead ( 200 is , {
 'the Type-the Content': ' text / html; charset = utf8 ' // if the output content with Chinese, the character set encoding 
}) 

Response.Write ( ' Hello Node.js' ) 

Response.End () 

}) the listen (Port, hostname, (). => {
 // parameters: port listener callback domain 
console.log ( `running at The Server IS AT: HTTP: // $ {hostname}: $ {}` port) 
})

Reptiles and may be used in conjunction with the output data crawling

Reptiles and may be used in conjunction with the output data crawling 
  const HTTP = the require ( 'HTTP' ) 

  const Port = 3000  

  const hostname = 'localhost' // 127.0.0.1 


  const Cheerio = the require ( 'Cheerio' ) 

  const Options = { 
    hostname: 'jx.1000phone.net' , 
    Port: 80 , 
    path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiZGZp' , 
    Method: 'the GET' , 
    headers: { 
      the Accept:'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
      'Accept-Encoding': 'gzip, deflate',
      'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
      'Cache-Control':' no-cache',
      Cookie: 'PHPSESSID=ST-22290-Uo8KnobsTgDO-TrQvhjA4TfoJI4-izm5ejd5j1npj2pjc7i3v4z',
      Host: 'jx.1000phone.net',
      Pragma: 'no-cache',
      'Proxy-Connection': 'keep-alive',
      Referer: 'http://jx.1000phone.net/teacher.php/Class/index',
      'Upgrade-Insecure-Requests': 1,
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': 0
    }
  };


  http.createServer((request,response) => {

    response.writeHead( 200, {
      'Content-Type': 'text/html;charset=utf8'
    })

    const req = http.get( options, (res) => {
      const { statusCode } = res;  // 获取状态码  1xx - 5xx
      const contentType = res.headers['content-type']; //File type text / JSON / HTML / XML 
    
      res.setEncoding ( 'UTF8'); // character encoding 
    
      // Core - Start 
      the let the rawData = '' ; 
      res.on ( 'Data', (the chunk) => + {the rawData the chunk =;}); // data registration 
      res.on ( 'end', () => { // data acquiring end 
        the try { 
    
          const $ = cheerio.load (the rawData) 
    
          $ ( . 'td.student A') each ( function (Item) { 
            Response.Write ( ` <H3> $ {$ ( the this ) .text ()} </ H3>`)
           }) 
          Response.End () 
        }the catch (E) { 
          console.error (e.message); 
        } 
      }); 
    
      // Core - End 
    }) ON ( 'error', (E) =>. { 
      console.error ( `Got error: $ {E } `.message); 
    }); 
    
    
    req.end () 

  }) the listen (port, hostname, (). => {
     // parameters: port listener callback domain 
    console.log (` running at The Server IS AT: HTTP: / / $ {hostname}: `$ {Port}) 
  })

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/zhaoqianguo/p/11446662.html