1. vs code 里面如何切换自定义终端?
2. 浏览器 vs node
异:
- node里面没有 BOM DOM
- node多了很多自带的api
同: - 都是chrome v8
- 都支持js
- 都支持 ECMA Script
3. 需求: sum这个方法, 我想三个参数 , 计算三个参数值?
- Node.js 命令行有时候用起来不方便
- 解决: 我们使用文件 .js
Node.js命令行退出
- two times ctrl+c
Node.js文件运行
node 文件名称(后缀可以不要)
键盘上下键可以前进和回退命令
自动监听(自动更新, 自动刷新)Node.js文件的更新和变化( 工具 nodemon supervisor)
使用淘宝镜像
- 工具安装
cnpm i nodemon -g (i==> install g ==> global) 推荐
cnpm i supervisor -g - 使用:
nodemon 文件名称
supervisor 文件名称 - 注意事项:
问题: supervisor 会出现死循环 ?
分析: 内容一致在改变
解决: vs code 开了自动保存
nvm 使用
- 安装:
nvm install vsersion
举例: nvm install v10.8.0 || nvm install 10.8.0 || nvm install latest(最新版本) - 切换Node.js版本
nvm use vsersion
举例: nvm install v10.8.0 - 查看当前电脑中 Node.js的所有版本
nvm list
前端模块化(面试题)
- CMD ( sea.js )
- AMD ( require.js )
- Common.js
- es6模块化
CMD 和 AMD
define 定义模块
Node.js中使用了Common.js规范(三类)- 内置的
Node.js内置了很多的模块
fs(文件系统)
path( 磁盘路径 )
http( 通信 )
使用;- 导入
const 变量名称 = require(‘模块名称’) - 使用模块的api
- 导入
- 第三方的
Node.js中第三方模块如何使用?https://www.npmjs.com/- 安装
- 初始化生成 package.json
- 安装对应的包
举例: npm i request -g/-D/-S
名词说明:
-g -global 全局
-D / --save-dev 开发环境
-S / -save 生产环境 - 导入
request 用来做数据请求的 - 使用
去看文档(www.npmjs.com)
- 安装对应的包
- 初始化生成 package.json
- 安装
- 自定义的
Node.js自定义模块步骤:- 定义
对象 函数 字符串 。。。
const age = {
name: ‘Yyb’,
age: 16
} - 导出
module.exports || exports
1.module.exports = {
age: age.age,
name: age.name
}(封装)
2.exports.age = age - 使用
const 变量名称 = require(‘模块名称’)
const age = require(‘age’) X 理由: age不是内置模块或是第三方模块
const age = require(’./age.js’)
console.log( age.name );
console.log( age.age ); - 扩展
需求: 安全性考虑 封装一下下 {}
自定义发布一个包(带有一定功能,),别人可以随意下载, 使用
操作流程
1.创建一个文件夹
2.创建pageage.json
Yarn init / npm init /cnpm init
快速创建 npm init -y / yarn init -y /cnpm init -y
3.注册npm仓库
www.npmjs.com注册一个账号
命令行执行 npm adduser(必须确保你当前的源是npmjs,使用nrm来切换源)
4.上传包
npm publish
注意:
1.npm源切换 nrm. npm i nrm -g
2.注意:npm账号需要邮箱认证
- 定义
- 内置的
前端的环境
- 开发环境
- 生产环境
- 测试环境
- 预发布环境
- 上线环境
问题: Node.js中请求数据, 需要跨域吗?
不需要跨域的
同源策略
- 为什么会出现跨域
开发中会有不同的域名和端口等出现?我们需要去获取他们的内容 - 浏览器如何组织跨域
浏览器具有安全策略 —》 同源策略实现 - 跨域的范围是?
浏览器
问题: 为什么要有 package.json?
分析: 帮助我们记录第三方的内容
即使没有node_modules也可以下载
自定义模块的发布
package.json —> 当前项目的依赖包 兵哥
package-lock.json —> 当前项目依赖包的具体信息 兵哥的具体信息
Node.js是单线程
主线程
异步队列: Node.js中异步任务, 放在异步队列
注意: 优先执行主线程中任务, 主线程任务结束后, 再去执行异步队列中任务
http
- 使用Node.js实现一个web服务器
- Node.js中模块的api很多时候可以连缀(链式调用)
- http
- createServer 创建一个web静态服务器
- listen 是用来监听当前服务器
- 名词解释
- port 端口
- hostname: 域名
- request 请求, Node.js请求谁
- response 回应]
- data 数据
- encoding; 编码方式 utf8 默认utf8
- write(data,encoding) 给前台发送一个内容
- end() 发送已经结束了
- 坑
中文乱码问题
解决方案一:
response.writeHead(200,{
'Content-Type': 'text/html; charset=UTF-8'
})
解决方案2:
response.write('<head> <meta charset="UTF-8"> </head>')
例:
const http = require('http');
const port = 8090;
const hostname = 'localhost' // 127.0.0.1
// 格式: http.createServer(callback).listen(prot,hostname,callback)
http.createServer(function(request,response){
// response.writeHead(statusCode,options)
response.writeHead(200,{
'Content-Type': 'text/html'
})
response.write('<head> <meta charset="UTF-8"> </head>')
response.write('<h1> yanyabing : 最帅的</h1>');
response.end()
}).listen(port,hostname,function(){
//在后端控制台输出
console.log('====================================');
console.log(`服务器运行在: https: //${hostname}:${port}`);
console.log('====================================');
})
url
url:
用来做 浏览器 地址 解析的
api:
parse : String --> Object
format: Object —> String
resolve: url拼接
完整的url构成:
https: // www.web-yyb.top: 8080 / vue / index.html ? a=1&b=2#a=10
协议: https
域名: www.web-yyb.top
端口: 8080
路径: www.web-yyb.top: 8080 / vue / index.html
查找字符串: a=1&b=2
哈希: #a=10
例:
const url = require('url')
const urlStr =
'https://www.web-yyb.top:8080/vue/index.html?a=1&b=2#a=10'
const urlObj = url.parse(urlStr)
const obj = {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.web-yyb.top:8080',
port: '8080',
hostname: 'www.web-yyb.top',
hash: '#a=10',
search: '?a=1&b=2',
query: 'a=1&b=2',
pathname: '/vue/index.html',
path: '/vue/index.html?a=1&b=2',
href: 'https://www.web-yyb.top:8080/vue/index.html?a=1&b=2#a=10' }
/*
Url {
protocol: 'https:',
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#a=10',
search: '?%20a=1&b=2',
query: '%20a=1&b=2',
pathname:
'%20//%20www.web-yyb.top:%208080%20/%20vue%20/%20index.html%20',
path:
'%20//%20www.web-yyb.top:%208080%20/%20vue%20/%20index.html%20?%20a=1&b=2',
href:
'https:%20//%20www.web-yyb.top:%208080%20/%20vue%20/%20index.html%20?%20a=1&b=2#a=10' }
*/
// format
const str = url.format(obj)
console.log( urlObj )
console.log('====================================');
console.log(str);
console.log('====================================');
// resolve
/* https://www.web-yyb.top/a/index.html
https://www.web-yyb.top/a/demo.html
https://www.web-yyb.top/b/demo.html
*/
const astr = 'https://www.web-yyb.top/a/index.html '
const bstr = url.resolve(astr,'b/demo.html')
//https://www.web-yyb.top/a/b/demo.html
const cstr = url.resolve(astr,'../b/demo.html')
//https://www.web-yyb.top/a/b/demo.html
console.log('====================================');
console.log(bstr);
console.log(cstr);
console.log('====================================');
querystring
qs:
1.干什么:
进行string 和 object 的格式
2.功能:
类似JSON.parse || JSON.stringify
3.api:
parse
stringify Object —> String
escape
unescape
名词
encoding 编码
unencoding 解码
escape 中文编码
unescape 中文解码
例:
const qs = require('querystring')
const qsObj = {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.web-yyb.top:8080',
port: '8080',
hostname: 'www.web-yyb.top',
hash: '#a=10',
search: '?a=1&b=2',
query: 'a=1&b=2',
pathname: '/vue/index.html',
path: '/vue/index.html?a=1&b=2',
href: 'https://www.web-yyb.top:8080/vue/index.html?a=1&b=2#a=10'
}
//1. stringify
const qsStr = qs.stringify(qsObj)
// console.log( qsStr )\
// 2. parse
const obj = qs.parse(qsStr)
// console.log( obj )
// 3. escape
const cityUrl = 'https://www.web-yyb.top?city=北京'
const escapeStr = qs.escape(cityUrl)
console.log( escapeStr )
//4. unescape
const cityEsUrl =
'https%3A%2F%2Fwww.web-yyb.top%3Fcity%3D%E5%8C%97%E4%BA%AC'
console.log( qs.unescape(cityEsUrl) )
http-get
get方法
http
格式:
http.get(url,callback)
on
原生js如何触发事件
DOM.onclick = function(){}
名词解释:
DOM: 节点
on: 添加事件的形式
click: 事件类型
function(){}: 事件处理程序
chunk: 分片
try {} catch{} 高级编程 错误信息捕获
例:
const http = require('http')
http.get('http://api.douban.com/v2/movie/in_theaters',function( res ){
//错误信息的报出
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 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 data = '' //保存数据
// res.on() // 事件执行
res.on('data',(chunk)=>{
data+=chunk; //将数据流累加起来
})
res.on('end',()=>{
try{
console.log( data )
}catch(e){
console.error( e.message )
}
})
}).on('error',(e)=>{
console.error(`Got error: ${e.message}`);
})
##http-spider
后端爬虫:
数据抓取 —》 数据清洗 —》 数据格式整理—》 发送前台(web服务器)
例:
const http = require('http');
const cheerio = require('cheerio')
const port = 8090
const options = {
hostname: 'jx.1000phone.net',
port: 80,
path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiYmho',
method: 'GET',
headers: {
'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',
'Connection': 'keep-alive',
'Cookie': 'PHPSESSID=ST-108933-uAIuYe-dtgk1qgGt984ihvl53Lk-izm5ejd5j1npj2pjc7i3v4z',
'Host': 'jx.1000phone.net',
'Pragma': 'no-cache',
'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/73.0.3683.86 Safari/537.36',
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': ''
}
}
http.createServer((request,response)=>{
response.writeHead(200,{
'Content-Type': 'text/html;charset=UTF-8'
})
const req = http.get(options,( res )=>{
//错误信息的报出
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
}
if (error) {
// console.error(error.message);
// Consume response data to free up memory
res.resume();
return;
}
//数据处理
res.setEncoding('utf8'); // 数据的编码格式
let data = '' //保存数据
// res.on() // 事件执行
res.on('data',(chunk)=>{
data+=chunk; //将数据流累加起来
})
res.on('end',()=>{
try{
console.log( data )//拿到了整个网页 --》 数据清洗 --> 工具 --》 cheerio
const $ = cheerio.load(data)
$('td.student a').each(function(i,v){
response.write(`<h3> ${ $(this).text() } </h3>`)
})
response.end()
}catch(e){
// console.error( e.message )
}
})
}).on('error',(err)=>{
// console.log( 'err',err )
})
}).listen(port,()=>{
console.log('服务器运行在: http://localhost:8090')
})