1 浏览器组成部分
浏览器引擎:
①负责在用户界面和渲染引擎之间传送指令
②在客户端本地缓存中读写数据
③浏览器各部分通信的核心
渲染引擎:负责解析 html 和 css
js引擎:负责解析 JavaScript 代码
不同的浏览器使用不同的JavaScript引擎:
① Chrome 浏览器 => V8
② Firefox 浏览器 => OdinMonkey(奥丁猴)
③ Safri 浏览器 => JSCore
④ IE 浏览器 => Chakra(查克拉)
不同的浏览器使用不同的渲染引擎:
① Chrome 浏览器 => webkit, blink
② Firefox 浏览器 => Gecko
③ Safri 浏览器 => webkit
④ IE 浏览器 => Trident
2 Nodejs
2.1 Nodejs 概念
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境
2.2 global 模块
global 模块是 nodejs 中的全局模块,在使用其中的变量和方法时可以直接使用而不需要引入。
除了 global 模块之外,其余的模块都需要加载。
2.3 fs 模块
1.加载(引入)方式
const fs = require("fs");
2.读取文件
语法:fs.readFile(path[, options], callback)
const fs = require('fs')
fs.readFile("data.txt",function(err, data){
if(err){console.log(err);}
console.log(data);
});
const fs = require('fs')
fs.readFile("data.txt", "utf8",function(err, data){
if(err){console.log(err);}
console.log(data);
});
3.写文件
语法:fs.writeFile(file, data[, options], callback)
const fs = require('fs')
fs.writeFile("data.txt", "hello world",function(err, data){
if(err){
console.log(err);
}
});
const fd = fs.readFile("data.txt",'utf-8',function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
需要注意的是,给文件写入内容时,会把原来的内容覆盖掉。
4.追加文件
语法:fs.appendFile(path, data[, options], callback)
const fs = require('fs')
fs.appendFile("data.txt", "这是追加的内容",function(err, data){
if(err){
console.log(err);
}
});
const fd = fs.readFile("data.txt",'utf-8',function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
5.路径动态拼接
代码运行时,会以执行 node 命令时所处的目录,动态拼接出被操作文件的完整路径,因此在使用 fs 模块操作文件时,很容易出现路径动态拼接错误的问题。为了解决这个问题,我们可以使用__dirname 来获取当前文件所在的绝对路径。
const fs = require('fs')
console.log(`${__dirname}/nodejs.js`);
2.4 path 路径模块
1.概念
path 模块是官方提供用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。如果要在 JavaScript 代码中使用该模块,则需要提前导入,
const path = require('path')
2.path.join() 方法
path.join() 方法能够将所有给定的 path 片段连接在一起,然后规范化生成路径
const path = require('path');
console.log( path.join('a', 'b', 'c') ); // a/b/c
console.log( path.join('a', '/b/', 'c') ); // a/b/c
console.log( path.join('a', '/b/', 'c', 'index.html') ); // a/b/c/index.html
console.log( path.join('a', 'b', '../c', 'index.html') ); // a/c/index.html
需要注意的是,../ 会注释掉前面的路径,因此最后一个路径拼接才会没有 /b 。
3.path.basename() 方法
语法:path.basename(path[,ext])
其中,path <string> 为必选参数,表示一个路径的字符串 ; ext <string> 为可选参数,表示可选的文件扩展名 ;返回<string> 表示路径中的最后一部分
const path = require('path');
console.log( path.basename('index.html') ); // index.html
console.log( path.basename('a/b/c/index.html') ); // index.html
console.log( path.basename('a/b/c/index') ); // index
console.log(path.basename('/a.jpg')) // a.jpg
即便是没有文件扩展名也会返回路径中的最后一个值。
4.path.extname() 方法
使用 path.extname() 方法,可以获取路径中的扩展名部分。
const path = require('path');
console.log( path.extname('index.html') ); // .html
console.log( path.extname('a.b.c.d.html') ); // .html
console.log( path.extname('asdfas/asdfa/a.b.c.d.html') ); // .html
console.log( path.extname('add.abc') ); // .adsf
console.log( path.extname('add') ); //
需要注意的是,对于没有后缀的路径,返回结果为空串。
2.5 http 模块
(1)定义
http 模块是 Node.js 官方提供的、用来创建web 服务器的模块。
(2)创建最基本的 web 服务器
创建最基本的 web 服务器有以下四个步骤:
① 导入 http 模块
const http = require('http');
② 创建 web 服务器实例
const server = http.createServer();
③ 启动服务器
server.listen(3000, () => {
console.log('my server start work');
});
④ 为服务器实例绑定request 事件,监听客户端的请求
server.on('request', () => {
// 这里要处理客户端的请求
console.log('hello html');
});
(3)根据不同的url响应不同的html内容
const http = require('http');
const server = http.createServer();
server.listen(3000, () => console.log('my server running'));
server.on('request', (req, res) => {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
const url = req.url // 获取请求的url地址
if (url === '/' || url === 'index.html') {
res.end('<h1>首页</h1>');
} else if (url === '/about.html') {
res.end('<h1>关于我们</h1>');
} else {
res.end('<h1>404</h1>');
}
});
3 模块化
3.1 Node.js 中模块的分类
① 内置模块(内置模块是由 Node.js 官方提供的,例如 fs、path、http 等)
② 自定义模块(用户创建的每个 .js
文件,都是自定义模块)
③ 第三方模块(由第三方开发出来的模块,并非官方提供的内置模块,也不是用户创建的自定义模块,使用前需要先下载)
3.2 模块加载
在 nodejs 中,无论是什么模块,都需要使用 require() 方法去加载,加载自定义模块时,需要加 ./ (可省略后缀 .js)
// 加载核心模块
const fs = require('fs');
// 加载第三方模块
const express = require('express');
// 加载自定义模块
const custom = require('./custom');
4 同源和跨域
4.1 定义
同源指的是两个URL地址具有相同的协议地址、主机名、端口号,反之则是跨域。
4.2 同源策略
浏览器的同源策略规定:不允许非同源的 URL 之间进行资源的交互。
同源策略是浏览器提供的一个安全功能,若脱离浏览器则不存在同源策略。
具体来说,我们可以使用 Ajax 技术发起跨域请求,并且对应的服务器也能正常返回响应数据,但浏览器会对该响应数据进行拦截,因此最终无法被页面获取。
4.3 解决方案
代理服务器 和 CORS 是实现跨域数据请求的两种技术方案。