1. supervisor工具
作用:supervisor 可以改动服务代码式不用重启node服务 只需刷新浏览器新代码就会生效
安装方式:安装命令 npm(外网慢)/cnpm(国内快) install -g supervisor
2. 写流剖析
var fs = require('fs');
// 创建一个写流方法的过程 如果有文件则会往文件中添加内容 没有该文件则会新增该文件并向其中添加内容
var writeStream = fs.createWriteStream('input.txt');
// 自定义要添加的内容
var str = ''
for(var i = 0; i<100; i++) {
str += '青青草原\n'
}
// 开始写入内容 第一个参数是要写入的内容 第二个是编码格式
writeStream.write(str, 'utf-8')
// 标记写流结尾
writeStream.end();
// 标记写流完成
writeStream.on('finish', function(data) {
console.log(data)
})
3. 读流剖析
var num = 0 //监听读取内容段数
// 创建一个读流的方法
var readStream = fs.createReadStream('./input.txt')
// 监听读流的过程
readStream.on('data', function(err, data) {
num++
})
// 读流结束
readStream.on('end', function() {
console.log(num)
})
// 监听读流过程中发生的错误
readStream.on('error', function(err) {
})
4.管道流
var fs = require('fs');
// 创建一个写流的方法
var writeSteam = fs.createWriteStream('./file/out.txt')
var num = 0
// 创建一个读流的方法
var readStream = fs.createReadStream('./input.txt')
// 管道流
readStream.pipe(writeSteam) // 利用管道流实现文件复制
5.路由剖析
var url = require('url') // url是node的内置模块可以直接使用
// var path = "https://www.baidu.com/index.html#index?md=mobile"
// var path2 = "https://www.baidu.com/index.html?md=mobile"
// url地址中#后面的值叫哈希值 hash
// url地址?后面的值叫search值 一般是key=value&key=value的形式这种形式的参数叫query参数
// parse作用是将路由链接解析成了一个对象 对象中会包含路由对象的所有信息
带#号
var path = "https://www.baidu.com/index.html#index?md=mobile"
var pathname = url.parse(path)
console.log(pathname)
// {
// protocol: 'https:', 连接中的传输协议
// slashes: true,
// auth: null,
// host: 'www.baidu.com', 连接中的域名
// port: null,
// hostname: 'www.baidu.com',连接中的域名
// hash: '#index?md=mobile', 哈希值
// search: null,
// query: null,
// pathname: '/index.html',
// path: '/index.html',
// href: 'https://www.baidu.com/index.html#index?md=mobile'
// }
无#号
var path2 = "https://www.baidu.com/index.html?md=mobile"
var pathname2 = url.parse(path2)
console.log(pathname2)
// {
// protocol: 'https:',
// slashes: true,
// auth: null,
// host: 'www.baidu.com',
// port: null,
// hostname: 'www.baidu.com',
// hash: null,
// search: '?md=mobile', 链接中?及后面的部分
// query: 'md=mobile',
// pathname: '/index.html', 地址名称
// path: '/index.html?md=mobile',
// href: 'https://www.baidu.com/index.html?md=mobile'
// }
练习: 使用http/url/fs混用搭建简单服务器
var http = require('http');
var fs = require('fs');
var url = require('url');
var path = require('path');
http.createServer(function(req, res) {
var pathname = url.parse(req.url).pathname;
// 获取文件的扩展名
var extname = path.extname(pathname);
pathname = pathname == '/' ? '默认渲染的html文件的地址' : pathname
//根据传入的后缀名返回对应的文件类型
function getExtType (ext) {
var str = ''
switch (ext) {
case '.html':
str = "text/html"
break;
case '.css':
str = "text/css"
break;
case '.js':
str = "text/javascript"
break;
}
return str
}
var type = getExtType(extname)
fs.readFile('./static' + pathname, function(err, data) {
if(err) {
fs.readFile('要渲染的错误文件的文件地址', function(err, errPage) {
res.writeHead(404, {"Content-type": `${type};charset=utf-8`})
res.write(errPage)
res.end()
})
} else {
res.writeHead(200, {"Content-type": `${type};charset=utf-8`}) //${type}使用字符模板
res.write(data)
res.end()
}
})
}).listen(3000, function() {
})