当前位置: 首页 > news >正文

怎样做企业手机网站建设赣州市建设局网站

怎样做企业手机网站建设,赣州市建设局网站,最新新闻热点事件直播,方象科技的服务范围用 zlib 来实现 gzip 压缩 服务端优化都是#xff1a;压缩 缓存 前端可以通过 webpack 插件进行压缩 gzip 根据替换来实现的#xff0c;重复率越高#xff0c;压缩后的结果越小 const zlib require(zlib); const fs require(fs); const path …用 zlib 来实现 gzip 压缩 服务端优化都是压缩 缓存 前端可以通过 webpack 插件进行压缩 gzip 根据替换来实现的重复率越高压缩后的结果越小 const zlib require(zlib); const fs require(fs); const path require(path);方式一读取文件压缩如果文件大的话都读取到内存中耗内存 zlib.gzip(fs.readFileSync(path.resolve(__dirname, ./65/1.txt)), (err, data) {fs.writeFileSync(path.resolve(__dirname, ./65/2.txt.gz), data); });方式二用转化流可读可写的方式服务端的文件 压缩 客户端 fs.createReadStream(path.resolve(__dirname, ./65/1.txt)).pipe(zlib.createGzip()).pipe(fs.createWriteStream(path.resolve(__dirname, ./65/2.txt.gz)));新建 1.txt 文件用来测试压缩里面写的都是1大概有个 10k 的样子两种方式执行之后都会生成压缩文件 2.txt.gz如下 代码实现 http-server 里的 gzip 压缩 下面实现 http-server 里的 gzip 压缩 实现思路就是根据请求头 header [accept-encoding] 里是否包含 gzip 来判断浏览器是否支持 gzip 压缩如果支持就创建转化流处理。 在上一节的 server.js 里面添加代码如下 // 核心模块 const http require(http); const path require(path); const url require(url); const fs require(fs).promises; const { createReadStream, createWriteStream, readFileSync } require(fs);// 第三方模块 const ejs require(ejs); // 服务端读取目录进行渲染 const mime require(mime); const chalk require(chalk); const debug require(debug)(server); // 根据环境变量来进行打印 process.env.EDBUG debug(hello kaimo-http-server);// 同步读取模板 const template readFileSync(path.resolve(__dirname, template.ejs), utf-8);class Server {constructor(config) {this.host config.host;this.port config.port;this.directory config.directory;this.template template;}async handleRequest(req, res) {let { pathname } url.parse(req.url);// 需要对 pathname 进行一次转义避免访问中文名称文件找不到问题console.log(pathname);pathname decodeURIComponent(pathname);console.log(pathname);// 通过路径找到这个文件返回let filePath path.join(this.directory, pathname);console.log(filePath);try {// 用流读取文件let statObj await fs.stat(filePath);// 判断是否是文件if (statObj.isFile()) {this.sendFile(req, res, filePath, statObj);} else {// 文件夹的话就先尝试找找 index.htmllet concatFilePath path.join(filePath, index.html);try {let statObj await fs.stat(concatFilePath);this.sendFile(req, res, concatFilePath, statObj);} catch (e) {// index.html 不存在就列出目录this.showList(req, res, filePath, statObj, pathname);}}} catch (e) {this.sendError(req, res, e);}}// 列出目录async showList(req, res, filePath, statObj, pathname) {// 读取目录包含的信息let dirs await fs.readdir(filePath);console.log(dirs, -------------dirs----------);try {let parseObj dirs.map((item) ({dir: item,href: path.join(pathname, item) // url路径拼接自己的路径}));// 渲染列表这里采用异步渲染let templateStr await ejs.render(this.template, { dirs: parseObj }, { async: true });console.log(templateStr, -------------templateStr----------);res.setHeader(Content-type, text/html;charsetutf-8);res.end(templateStr);} catch (e) {this.sendError(req, res, e);}}gzip(req, res, filePath, statObj) {if (req.headers[accept-encoding] req.headers[accept-encoding].includes(gzip)) {// 给响应头添加内容编码类型头告诉浏览器内容是什么编码类型res.setHeader(Content-Encoding, gzip);// 创建转化流return require(zlib).createGzip();} else {return false;}}// 读取文件返回sendFile(req, res, filePath, statObj) {// 设置类型res.setHeader(Content-type, mime.getType(filePath) ;charsetutf-8);// 读取文件进行响应// 先判断浏览器是否支持 gzip 压缩let gzip this.gzip(req, res, filePath, statObj);if (gzip) {createReadStream(filePath).pipe(gzip).pipe(res);} else {createReadStream(filePath).pipe(res);}}// 专门处理错误信息sendError(req, res, e) {debug(e);res.statusCode 404;res.end(Not Found);}start() {const server http.createServer(this.handleRequest.bind(this));server.listen(this.port, this.host, () {console.log(chalk.yellow(Starting up kaimo-http-server, serving ./${this.directory.split(\\).pop()}\r\n));console.log(chalk.green( http://${this.host}:${this.port}));});} }module.exports Server;我们启动服务去访问 http://localhost:3000/1.txt kaimo-http-server跟没有压缩的对比如下
http://www.dnsts.com.cn/news/207509.html

相关文章:

  • 钟祥网站开发建站模板安装视频教程全集
  • 微信网站开发怎么做怎么优化标题和关键词排名
  • jsq项目做网站2018年深圳建设网站公司
  • 驻马店北京网站建设深圳制作企业网站
  • 网站 美食频道 建设湖南省郴州市旅游景点介绍
  • 注册一个网站多少钱?免费设计素材下载
  • 百度收录快的网站注册公司注册地址
  • 网站开发大学一般设计网站页面用什么软件
  • 新会网站建设销售怎么做
  • 外贸网站建设服务器wordpress 产品类标题
  • 找建筑工作哪个网站好网站从哪里找的
  • 洛阳网站建设兼职网站可做2个首页吗
  • 网站备案核验单怎么填怎么注册公司企业微信
  • 产品如何做网站推广wordpress 海淘主题下载地址
  • 专门做宠物食品的网站网站有备案 为企业
  • 佛山市企业网站seo联系方式做跨境电商有没推荐的网站
  • 长沙装修网站排名房地产销售计划
  • 服装展示网站源码花都网站建设公司
  • 怎样批量做地级市网站餐饮行业做微信网站有什么好处
  • 企业网站代码网站推广公司运营模式
  • 东莞怎样做网站建设一小时学会网站建设
  • 网站建设运营企划案阿里巴巴网站建设教程视频
  • 龙岩市住房与城乡建设部网站超炫酷网站欣赏
  • 北京平台网站建设公司基于php网站开发步骤
  • 免费网站推广网站破解版南阳网站优化公司
  • 怎么样才能找到网站后台网址交换链接适合哪些网站
  • 唐山企业做网站页面设计图
  • 网站设计定位网站建设的财务计划
  • 纪检网站建设计划书标书制作教程视频网站
  • 写一张营销型网站页面多长时间网站建设开发实训报告总结