node.js.1

  1. vs code 里面如何切换自定义终端?
    命令面板(ctrl+shift+p)中,输入select选择第一条
    之后选择目标shell

  2. 浏览器 vs node
    异:

  3. node里面没有 BOM DOM

  4. node多了很多自带的api
    同:

  5. 都是chrome v8

  6. 都支持js

  7. 都支持 ECMA Script

  8. 需求: 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规范(三类)

  1. 内置的
  2. 第三方的
  3. 自定义的

前端的环境
开发环境
生产环境
测试环境
预发布环境
上线环境
问题: Node.js中请求数据, 需要跨域吗?
不需要跨域的

同源策略

为什么会出现跨域
开发中会有不同的域名和端口等出现?我们需要去获取他们的内容
浏览器如何组织跨域
浏览器具有安全策略 —》 同源策略实现
跨域的范围是?
浏览器
问题: 为什么要有 package.json?
分析: 帮助我们记录第三方的内容
即使没有node_modules也可以下载

自定义模块的发布
package.json —> 当前项目的依赖包 兵哥

package-lock.json —> 当前项目依赖包的具体信息 兵哥的具体信息

Node.js是单线程

​ 主线程

​ 异步队列: Node.js中异步任务, 放在异步队列

​ 注意: 优先执行主线程中任务, 主线程任务结束后, 再去执行异步队列中任务

Node.js自定义模块步骤:

  1. 定义
    对象 函数 字符串 。。。
  2. 导出
    module.exports || exports
  3. 使用
    const 变量名称 = require(‘模块名称’)
  4. 扩展
    需求: 安全性考虑 封装一下下 {}
    */
    // 1. 定义
    const age = {
    name: ‘Yyb’,
    age: 16
    }
    // 2. 导出
    // module.exports = {
    // age: age.age,
    // name: age.name
    // }
    exports.age = age

//3. 使用
// const age = require(‘age’) X 理由: age不是内置模块或是第三方模块
const age = require(’./age.js’)
console.log( age.name );
console.log( age.age );
age.sex = ‘man’
// 自定义发布一个包(带有一定功能,),别人可以随意下载, 使用
/*
操作流程
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内置了很多的模块
fs(文件系统)
path( 磁盘路径 )
http( 通信 )
使用;

  1. 导入
    const 变量名称 = require(‘模块名称’)
  2. 使用模块的api
    */
    const path = require(‘path’)
    console.log(path.resolve(__dirname)) //E:\1901\01-node.js\day01\02-module
    // 需求: 返回上一级?
    console.log( path.resolve(__dirname,’…/’))
    console.log( path.join(__dirname,’…/’))

/*
Node.js中第三方模块如何使用?https://www.npmjs.com/

  1. 安装
  2. 初始化生成 package.json
  3. 安装对应的包
    举例: npm i request -g/-D/-S
    名词说明:
    -g -global 全局
    -D / --save-dev 开发环境
    -S / -save 生产环境
  4. 导入
    request 用来做数据请求的
  5. 使用
    去看文档(www.npmjs.com)
    */
    const request = require(‘request’);
    request(‘https://api.douban.com/v2/movie/in_theaters’,function(error,response,body){
    console.log(’’);
    console.log(‘error’,error)
    // console.log(‘response’,response);
    console.log(‘body’,JSON.parse(body))
    console.log(’
    ’);
    })

/*
总结:

  1. 使用Node.js实现一个web服务器
  2. Node.js中模块的api很多时候可以连缀(链式调用)
  3. http
  4. createServer 创建一个web静态服务器
  5. listen 是用来监听当前服务器
  6. 名词解释
  7. port 端口
  8. hostname: 域名
  9. request 请求, Node.js请求谁
  10. response 回应]
  11. data 数据
  12. encoding; 编码方式 utf8 默认utf8
  13. write(data,encoding) 给前台发送一个内容
  14. end() 发送已经结束了

  15. 中文乱码问题
    解决方案一:
    response.writeHead(200,{
    ‘Content-Type’: ‘text/html; charset=UTF-8’
    })
    解决方案2:
    response.write(’ ‘)
    */
    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(’ ‘)
    response.write(’

    yanyabing : 最帅的杭州H5讲师

    ‘);
    response.end()
    }).listen(port,hostname,function(){
    //在后端控制台输出
    console.log(’’);
    console.log(服务器运行在: https: //${hostname}:${port});
    console.log(’
    ’);
    })

/*
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(’
’);

/*
qs:
干什么:
进行string 和 object 的格式
功能:
类似JSON.parse || JSON.stringify
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) )

/*
get方法
http
格式:
http.get(url,callback)
on
原生js如何触发事件
DOM.onclick = function(){}
名词解释:
DOM: 节点
on: 添加事件的形式
click: 事件类型
function(){}: 事件处理程序
chunk: 分片
try {} catch{} 高级编程 错误信息捕获
*/
// http.get(url,callback)
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});
})

/*
后端爬虫:
数据抓取 —》 数据清洗 —》 数据格式整理—》 发送前台
*/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值