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端口')
})
结果:启动程序,去浏览器访问
debug一下(要修改主程序地址),可以发现有两个请求
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操作
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('写入成功')
})
})
})
如何处理客户端get/post请求
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
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请求是客户端传递数据给服务端
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
结果:
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端口')
})
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端口')
})