node知识点整理

提问:
继承
继承的分类
call apply bind

  • 都可以改变this指向
  • apply:参数是一个数组
  • bind:第一个参数要求是this
    匿名函数
  • 减少命名的冲突

内置模块

Stream 流

  • 流 :指的是数据流 指的是数据分片传输
  • 数据可以实现非阻塞
  • gulp 【流式操作】
    流程
  • 读取文件
  • 创建压缩包
  • 将读取的数据流写入压缩包
  • 输出压缩包

自定义模块

创建模块
如:

导出模块
module.exports

导入模块

第三方模块

1.一般从 npmjs.com 网站上拉取
使用流程:
1.安装:

  • 先创建package.json 文件
  • npm i request -D
    -S :–save 生产环境
    -D :–save-dev 开发环境
    2.使用:
    request 是做数据请求的
    node中的数据请求不存在跨域问题

http-get 请求

  1. 先引入http模块
    const http = require (‘http’)
  2. http.get(‘url’,(res)=>{
    用法详情可以参考 npmjs.com
    })
const http = require( 'http' )
http.get('http://api.k780.com/?app=weather.city&cou=1&appkey=43880&sign=6a31f4fe1408d69bb0417b046eeae5b6&format=json',
(res) => {
const { statusCode } = res; // 获取请求状态码 200 301 302 303 304 404 500
const contentType = res.headers['content-type']; // 获取请求类型数据类型
// json数据的content-type 'content-type': 'application/json'
let error;
if (statusCode !== 200) { // 如果状态码不是200 ,输出报错信息
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}
if (error) { // 如果报错了,将报错信息存入日志
console.error(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8'); // 字符编码
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; }); // 通过data事件拼接数据流
res.on('end', () => { // 获取数据结束了
try { // 高级编程语法 捕获错误信息
console.log( rawData );
} catch (e) {
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});

http模块 (爬虫)

爬虫

  • 后端渲染的网站
  • 去某一网站爬取一段数据 对其进行 数据清洗->后端服务器->发送前端->渲染数据

第一步
引入http文件
第二部
在nodejs网站上复制粘贴http.get方法 删除里面已经请求数据的方式
第三部
设置options对象 里面的参数参考 nodejs.cn 网站里面http.get里面options的参数
第四部
修改options里面的所需爬取的 域名 以及路径 修改method方法 为GET
第五步
修改 headers 里面的参数 将需要爬虫的 Response Headers 里面的数据粘贴过来进行对象得修改

node.js 读取文件都是二进制流

toString 转二进制

后端服务器

1.web服务器【静态服务器】
2.api服务器 【暴露接口】
请求头报文

  1. general 请求基本信息
  2. response Headers 响应头
  3. request Headers 请求头
  4. 携带参数
  • query string paramters get请求
  • form data post 请求
web 服务器

以创建一个简单的静态服务器为例

第一步:引入http
const http = require(‘http’)

// 1.引入http
const http = require ('http');
const host='localhost'
const port= 8000;

// 创建一个服务

http.createServer((res)=>{
// 设置请求头
response.writeHead(200,{
   'Content-type':'text/html;charset=utf8'
})

//写要输入前端的东西

response.write('<h3>创建了一个静态服务器</h3>')
   
// 创建服务结束
response.end() 
   
}).listen(port,host,()=>{
   console.log('The server is running at : http://${host}:${port}')
}) 

api服务器

Node.js 中api服务器的创建 我们使用第三方库 express

后端解决跨域的问题

  • 设置请求头
  • 使用中间件 第三方的包 (cors)

设置请求头

const app=express();
const port=3000;
const host='localhost'
//创建接口
    app.get('/user',(req,res,next)=>{
    //该回调函数称为中间件
   
res.setHeader('Access-Control-Allow-Origin', '*');
res.json({
id: 1,
name: '孙宇',
class: '1905'
})
    })
    
app.listen( port,host,() => {console.log( 'The server is running at : http://${ host }:${ port }` )
})


使用中间件

const express = require( 'express' )
const app = express() // 得到一个app对象
const port = 3000
const host = 'localhost'
const cors = require( 'cors' )


app.use(cors({
"origin": "*",
"methods":"GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 200
}))
app.get( '/user', ( req,res,next ) => { // 回调函数我们称之为: 中间件 具有特定功能的一个函数
res.json({
id: 1,
name: '孙宇',
class: '1905'
})
})

app.get( '/demo', ( req,res,next ) => { // 回调函数我们称之为: 中间件 具有特定功能的一个函数
res.json({
id: 2,
name: '赵思涵',
class: '1905'
})

反向代理的基本原理

  • 后端请求数据
  • 将数据发送给前端
    需要引用两个模块
  • request :用来做数据请求
  • express :用来将数据发送给前端
const request = require( 'request' )//request 是用来做数据请求的
const express = require( 'express' )//express 是用来给前端发送数据的
const host = 'localhost'
const port = 3000
const app = express()
const url = 'https://m.lagou.com/listmore.json'
//创建接口
app.get('/position', ( req,res,next ) => {
res.setHeader('Access-Control-Allow-Origin', '*'); // 设置请求头跨域
// 数据请求
request( url, ( error,response,body ) => {
res.json( JSON.parse( body ) )
})
})
app.listen( port,host, () => {
console.log( `The server is running at: http://${ host }:${ port }`)
})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值