Chrome--V8
Firefox -- OdinMonkey(奥丁猴)
Safri -- JSCore
IE -- Chakra
其中,Chrome浏览器的V8解析引擎性能最好,推荐使用chrome

我们在文件夹里面设置一个记事本,记事本命名为1.js,命名完成后这个记事本会改变,将这个文件用记事本打开,后在里面编辑js代码,完成后保存
我们可以通过cd C:\Users\w5339\Desktop\node.js来切换到这个路径
const fs = require('fs')
fs.readFile(path[,options],callback)
参数1:必选参数,字符串,表示文件的路径
参数2:可选参数,表示已什么编码格式来读取文件
参数3:必选参数,文件读取完成以后,通过回调函数拿到读取的结果
加上【】就是可选参数
实例
const fs = require('fs');
fs.readFile('./files/11.txt','utf8',function(err,datastr){
console.log(err);
console.log('-----');
console.log(datastr);
})

const fs = require('fs');
fs.readFile('./files/11.txt','utf8',function(err,datastr){
if(err){
return console.log('读取文章失败'+err)
}
console.log('读取文章成功,内容是:' + datastr);
})
fs.writeFile(file,data[,options],callback)
参数1:必选参数,指定一个文件路径的字符串,表示文件的存放路径
参数2:必选参数,表示要写入的内容
参数3:可选参数,表示要以什么格式写入文件,默认是utf8
参数4:必选参数,文件写入完成后的回调函数
实例const fs = require('fs');
fs.writeFile('f:./files/1.txt','真乖','utf8',function(err,datastr){
//写入成功打印的是null,写入失败err的值是一个错误对象
//datastr无论成功还是是败都是undefined,写一个err就行了
console.log(err)
console.log('----------');
console.log(datastr)
})
const fs = require('fs');
fs.writeFile('./files/2.txt','哈哈哈','utf8',function(err){
if(err){
return console.log('写入文件失败' + err.message);
}
console.log('文件写入成功');
})

const fs = require('fs');
fs.readFile('./files/成绩.txt','utf8',function(err,datastr){
if(err){
return console.log('读取成绩失败' + err.message)
}
//split,slice,splice的区别
//split用来操作字符串,分割字符串,split(' ')根据空格分隔字符串,返回一个数组,不改变原字符串
//slice用来操作字符串,截取字符串,slice(1,4),不包括结束字符串,返回字符串,不改变原字符串
//splice用来操作数组,删除替换数组,splice(1,1,e)没有第三个参数就是删除,有就是替换,开始位置,个数,返回被删除或替换的元素,改变原数组
//1、将字符串按照空格分成数组
var attr = datastr.split(' ');
const attrnew = [];
attr.forEach(element => {
//item index
attrnew.push(element.replace('=',':'))
});
//3.将数组转换成字符串返回,join()默认使用逗号连接,join('=')使用=连接
const newstr = attrnew.join('\r\n')
fs.writeFile('./files/成绩ok.txt',newstr,'utf8',function(err){
if(err){
return console.log('写入文件失败')
}
console.log('写入文件成功')
})
})
cd…/返回上一层上一级…/同级./下一级/
在使用./或者…/开头的相对路径时,很容易出现路径动态拼接错误的问题
原因:代码在运行时,会以node命令所处的目录,动态拼接操作文件的路径

解决办法:提供一个绝对路径(完整的路径),移植性差(文件位置发生改变),不利于维护


完美的解决办法:
--dirname不会随着node路径的变化而变化,就表示当前文件的路径

为什么/files/1.txt,因为这里是路径拼接,而那个./files是在当前目录,返回上一层目录。
在JavaScript代码中,使用path模块来处理路径,先导入
const path = require('path')
path.join([...paths])
参数:
...paths路径片段的序列
返回值:
实例1:拼接时../会抵消前面的一个路径,./不会const path = require('path')
const str = path.join('/a','/b/c','../','/d')
console.log(str)

实例2:今后涉及到路径拼接的问题,都要使用path.join()方法进行处理不要直接使用+进行拼接,如果使用+,__dirname+'./files/1.txt'多了一个点就会报错,而使用path.join就不会报错,会自动屏蔽掉这个点const fs = require('fs')
fs.readFile(__dirname + './files/1.txt','utf8',function (err,datastr) {
if(err){
return console.log(err.message)
}
console.log(datastr)
})

const fs = require('fs')
fs.readFile(path.join(__dirname , './files/1.txt'),'utf8',function (err,datastr) {
if(err){
return console.log(err.message)
}
console.log(datastr)
})

path.basename(path[,ext])
参数解读:
path必选参数,表示一个路径的字符串
ext可选参数,表示文件扩展名
返回:表示路径中的最后一部分
-实例1:截取文件最后的文件名,带扩展名
const path = require('path')
const address = '/a/b/c/index.html'
const endadd = path.basename(address)
console.log(endadd);

截取文件名,不带扩展名const path = require('path')
const address = '/a/b/c/index.html'
const withoutext = path.basename(address,'.html')
console.log(withoutext)

path.extname(path)
参数
path必选参数,表示一个路径的字符串
返回:返回得到的扩展字符串
实例1const path = require('path');
const fpath = '/a/b/c/index.html'
const fext = path.extname(fpath)
console.log(fext)

时钟案例,练习fs和path


代码
//导入相应模块
const fs = require('fs')
const path = require('path')
//定义两个正则表达式,以//开始和结尾,结束的style的/需转义,否则就和开始和结尾重合了
//(\s匹配空白字符)(\S匹配非空白字符)(*匹配任意多次)
//定义两个正则表达式用于匹配style和script
const regstyle = /','')
//将这个文本写入文件,这个clock不能自己创建,path.join别忘记了
fs.writeFile(path.join(__dirname,'./clock/index.css'),newscss,'utf8',function (err) {
if(err){
return console.log('写入css文件失败' + err.message)
}
console.log('写入css文件成功')
})
}
//分离js
function resolvejs(htmlstr) {
const r2 = regscript.exec(htmlstr)
const newjs = r2[0].replace('','')
fs.writeFile(path.join(__dirname,'./clock/index.js'),newjs,'utf8',function (err) {
if(err){
return console.log('写入js文件失败' + err.message)
}
console.log('写入js文件成功')
})
}
//分离html
function resolvehtml(htmlstr) {
const newhtml = htmlstr.replace(regstyle,' ')
.replace(regscript,' ')
fs.writeFile(path.join(__dirname,'./clock/index.html'),newhtml,'utf8',function (err) {
if(err){
return console.log('写入html文件失败' +err.message)
}
console.log('写入html文件成功')
})
}
总结:两个注意点
什么是客户端,什么是服务器
http模块是node.js官方提供的,用来创建web服务器的模块,通过http.createServer()方法,就能方便把一台普通的电脑,变成一台web服务器,从而对外提供web资源服务
const http = require('http')
服务器和普通电脑的区别
IP地址
域名
域名服务器
举例
百度的域名为:www.baidu.com
ip地址为:182.61.200.7
两者都可以访问到百度的首页
端口号
实例
步骤:
//1.先导入HTTP模块
//2.创建一个web服务实例
//3.为web服务实例绑定request事件,监听客户端的需求
//4.启动服务器
const http = require('http')
const server = http.createServer()
server.on('request',function (req,res) {
console.log('someone visit our web server')
})
server.listen(8080,function () {
console.log('server running at http://127.0.0.1:8080')
})
req.url请求地址为端口号后面的地址
req.method打印的是发送请求的方法,现在是get方法,如果想要post方法的话,我们可以借助postman工具
const http = require('http')
const server = http.createServer()
server.on('request',(req,res)=>{
const str = `your request url is ${req.url},and request method is ${req.method}`
console.log(str)
})
server.listen(8081,(s)=>{
console.log('server running at http://127.0.0.1:8081')
})

const http = require('http')
const server = http.createServer()
server.on('request',(req,res)=>{
const str = `your request url is ${req.url},and request method is ${req.method}`
console.log(str)
*res.end(str)*
})
server.listen(8081,(s)=>{
console.log('server running at http://127.0.0.1:8081')
})


const http = require('http')
const server= http.createServer()
server.on('reqest',(req,res)=>{
const str = '你的地址是${req.url},你的请求方法是${req.method}'
//这个是显示在终端的
console.log(str)`
//加上下面这条语句就不会是解码错误
**res.setHeader('Content-Type','text/html;charset=utf-8')**
//这个是服务器返回给客户端的
res.end(str)
})
//启动服务器
server.listen(8082,()=>{
console.log('server running at http://127.0.0.1:8082')
})


代码
const http = require('http')
const server = http.createServer()
server.on('request',(req,res)=>{
//1.得到响应地址
const url = req.url;
//2.设置相应内容为默认值
let content = '404 Not Found
'
//3.根据地址判断是那个页面,返回相应的内容
if(url==='/'||url==='/index.html'){
content = 'index.html
'
}else if(url === '/about.html'){
content = 'about.html
'
}
//4.设置请求头,防止解码错误
res.setHeader('Content-Type','text/html; charset=utf-8')
//5.将相应内容返回给客户端
res.end(content)
})
server.listen(80,()=>{
console.log('your server running at 127.0.0.1:80')
})



存在问题,输入/clock/index.html不会渲染css,但是请求发送了,路径也打印了//导入web服务器
const http = require('http')
//导入读写文件模块
const fs = require('fs')
//导入路径拼接模块
const path = require('path')
//创建web服务器、
const server = http.createServer()
//监听web服务器的请求
server.on('request',function(req,res){
//3.1获取到客户端请求的路径,这个路径获取到之后的样子为/clock/index.html
const url = req.url
//3.2将请求过来的路径映射为具体的文件存放路径,就是为了方便我们下面读文件
const fpath = path.join(__dirname,url)
//3.3根据映射过来的路径读取文件的内容
fs.readFile(fpath,'utf8',function(err,datastr){
//4.1读取失败,向客户端相应固定的错误消息
if(err) return res.end('404 not found')
//4.2 读取成功,将读取成功的内容返回给客户端
res.end(datastr)
})
})
//启动web服务器
server.listen(8080,function(){
console.log('your server running at http://127.0.0.1:8080')
})

优化–两个问题



代码
//3.2将请求过来的路径映射为具体的文件存放路径,就是为了方便我们下面读文件
// const fpath = path.join(__dirname,url)
let fpath = ''
if(url === '/'){
//这个处理我们直接输入/的问题,默认访问为首页
fpath = path.join(__dirname,'/clock/index.html')
}else{
//这个是想让我们可以直接输入index.html,不用输入clock,但是此时我们如果在输入clock的话就会发生错误
fpath = path.join(__dirname,'/clock',url)
}
现实生活中的模块化
编程领域的模块化
好处
例如
好处
内置模块:内置模块是由node.js提供的,例如fs,path,http等
自定义模块:我们用户写的js文件就是自定义模块
第三方模块:由第三方开发出来的模块,并非官方的内置模块,也不是自定义的模块,使用前需要先下载
//内置模块
const fs = require('fs')
//自定义模块
const m1 = require('./13.执行被加载中的模块')
console.log(m1)
//第三方模块
const moment = require('moment')
使用require()方法加载其他模块时,会执行被加载模块中的代码,返回是一个空对象,模块作用域


注意
在使用require加载用户自定义模块时那个路径,可以省略.js,因为require会尝试补全一个路径




16
const age = 11;
module.exports.age = age;
module.exports.username = 'as'
module.exports.sayhello = function(){
console.log('hello')
}
17
const m =require('./16.使用module.exports向外面共享成员')
console.log(m)

注意点
const age = 11;
module.exports.age = age;
module.exports.username = 'as'
module.exports.sayhello = function(){
console.log('hello')
}
//让module.exports指向一个全新的对象,打印的是下面这个对象
module.exports = {
nickname:'小黑',
sayhi(){
console.log('丫丫')
}
}
const m =require('./16.使用module.exports向外面共享成员')
console.log(m)

console.log(module.exports === exports)

案例一
exports.username = 'xs'
module.exports = {
gender:'nan',
age:20
}


案例二
module.exports.username = 'xs'
exports = {
gender:'nan',
age:20
}


案例三
exports.username = 'zs'
module.exports.gender = '男'


案例四exports = {
username:'sx',
gender:'男'
}
module.exports = exports
module.exports.age = '11'

