使用nodejs中的http模块搭建服务器
 
    引入 http 模块
    创建 http 服务器
const http = require('http');

const server = http.createServer((req,res)=>{
  res.writeHead(200,{'content-type':'text/html'});
  res.end('<h1>hello 静静七分醉,</h1>');
})

server.listen(3000,()=>{
  console.log('监听了3000端口')
})

结果:启动程序,去浏览器访问

6 node.JS的get和post_Node.js

debug一下(要修改主程序地址),可以发现有两个请求

   6 node.JS的get和post_nodejs_02 使用http模块做一个简单的爬虫
 
简单爬虫实现
const https = require('https');
const fs = require('fs');

https.get('https://www.baidu.com',(res)=>{
  res.setEncoding('utf8');
  let html = '';
  res.on('data',chunk => {
    html += chunk;
  })
  res.on('end',()=>{
    console.log(html)
    fs.writeFile('./index.txt',html,(err) => {
      if(err) throw err;
      console.log('写入成功')
    })
  })
})

cheerio实现dom操作

    安装cheerio:npm install cheerio --save-dev
    引入cheerio:const cheerio = require('cheerio')
    通过 title元素获取其内容文本: const $ = cheerio .load (html );   console .log ($ ( 'title' ).text ());
const https = require('https');
const fs = require('fs');
const cheerio = require('cheerio')

https.get('https://www.baidu.com',(res)=>{
  res.setEncoding('utf8');
  let html = '';
  res.on('data',chunk => {
    html += chunk;
  })
  res.on('end',()=>{
    // console.log(html)
    const $ = cheerio.load(html); //把html代码加载进去,就可以实现jq的dom操作
    console.log($('title').text());
    fs.writeFile('./index.txt',html,(err) => {
      if(err) throw err;
      console.log('写入成功')
    })
  })
})
NodeJs核心模块api-路由与接口

如何处理客户端get/post请求

  url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

    urlString url 字符串
    parseQueryString 是否解析
    slashesDenoteHost
        - 默认为 false //foo/bar 形式的字符串将被解释成 { pathname: ‘//foo/bar' }
        - 如果设置成 true //foo/bar 形式的字符串将被解释成 { host: ‘foo', pathname: ‘/bar' }
 
const url = require('url');
console.log(url.parse('https://www.baidu.com/pub/api/v1/web/product/find_list_by_type?type=2'))
console.log(url.parse('https://www.baidu.com/pub/api/v1/web/product/find_list_by_type?type=2',true)) //解析query,以K V形式出现

结果:
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'api.xdclass.net',
  port: null,
  hostname: 'api.xdclass.net',
  hash: null,
  search: '?type=2',
  query: 'type=2',
  pathname: '/pub/api/v1/web/product/find_list_by_type',
  path: '/pub/api/v1/web/product/find_list_by_type?type=2',
  href: 'https://api.xdclass.net/pub/api/v1/web/product/find_list_by_type?type=2'
}

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'api.xdclass.net',
  port: null,
  hostname: 'api.xdclass.net',
  hash: null,
  search: '?type=2',
  query: [Object: null prototype] { type: '2' },
  pathname: '/pub/api/v1/web/product/find_list_by_type',
  path: '/pub/api/v1/web/product/find_list_by_type?type=2',
  href: 'https://api.xdclass.net/pub/api/v1/web/product/find_list_by_type?type=2'
}

get请求用于客户端向服务端获取数据,post请求是客户端传递数据给服务端

处理 get 请求
const url = require('url');
const http = require('http');

const server = http.createServer((req,res) => {
  let urlObj = url.parse(req.url,true);
  res.end(JSON.stringify(urlObj.query))
})

server.listen(3000,()=>{
  console.log('监听3000端口')
})

结果:

访问路径:http://localhost:3000/name=%E9%9D%99%E9%9D%99%E4%B8%83%E5%88%86%E9%86%89&age=20

结果:

    6 node.JS的get和post_nodejs_03

处理 post 请求
const url = require('url');
const http = require('http');

const server = http.createServer((req,res) => {
  let postData = '';
  req.on('data',chunk => {
    postData += chunk;
  })
  req.on('end',()=>{
    console.log(postData)
  })
  res.end(JSON.stringify({
    data:'请求成功',
    code:0
  }))
})

server.listen(3000,()=>{
  console.log('监听3000端口')
})
整合 get/post 请求
 
 
const url = require('url');
const http = require('http');

const server = http.createServer((req, res) => {
  console.log(2)
  if (req.method === 'GET') {
    let urlObj = url.parse(req.url, true);
    res.end(JSON.stringify(urlObj.query))
  } else if (req.method === 'POST') {
    let postData = '';
    req.on('data', chunk => {
      postData += chunk;
    })
    req.on('end', () => {
      console.log(postData)
    })
    res.end(JSON.stringify({
      data: '请求成功',
      code: 0
    }))
  }

})

server.listen(3000, () => {
  console.log('监听3000端口')
})