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

德尔普网站建设站长工具是什么意思

德尔普网站建设,站长工具是什么意思,域名 空间 网站制作,李志自己做网站目录 一、Path模块 二、fs模块 2.1、fs同步读取文件fs.readFileSync() 2.2、fs异步读取文件fs.readFile() 2.3、异步写入文件内容fs.writeFile() 三、Http模块 四、模块化 4.1、CommonJs的导入导出 4.2、ES6的导入导出 五、了解global和this 六、Sort()应用(数组排序…

目录

一、Path模块

二、fs模块

2.1、fs同步读取文件fs.readFileSync()

2.2、fs异步读取文件fs.readFile()

2.3、异步写入文件内容fs.writeFile()

三、Http模块

四、模块化

4.1、CommonJs的导入导出

4.2、ES6的导入导出

五、了解global和this

六、Sort()应用(数组排序)

前言:

打开cmd窗口使用node -v检查node版本,最好13+,

cmd或者集成终端下运行项目:node xx.js

官网文档: https://nodejs.cn/api-v16/fs.html

一、Path模块

__dirname是Node提供的特殊变量:可获取当前文件所在路径

path.join():拼接完整路径

const path=require('path');//引入path模块,引入后才能使用对应的功能
console.log(__dirname);//__dirname是Node提供的特殊变量:可获取当前文件所在路径
//输出:C:\Users\hp\Desktop\node
let ret=path.join(__dirname,'hello.txt');//拼接出文件的完整路径(含文件名)
console.log(ret);
//输出:C:\Users\hp\Desktop\node\hello.txt
let ret2=path.join(__dirname,'modules','m1.js');//获取m1.js的路径
console.log(ret2);
//输出:C:\Users\hp\Desktop\node\modules\m1.js

二、fs模块

2.1、fs同步读取文件fs.readFileSync()

const fs=require('fs');//fs 文件操作系统
const path=require('path');
let filePath=path.join(__dirname,'hello.txt')
let content =fs.readFileSync(filePath,'utf8');//fs.readFileSync(文件路径)
// 输出:<Buffer e8 bf 99...>   Buffer是Node在内存暂存数据的方式,需要加‘utf8’转码/结果.toString()
// 转码后输出:这是一个文本文件(hello.txt的内容)
console.log(content);
console.log('END--------');

2.2、fs异步读取文件fs.readFile()

const fs=require('fs');//fs 文件操作系统
const path=require('path');
let filePath=path.join(__dirname,'hello.txt')
fs.readFile(filePath,'utf8',(err,data)=>{if(err){console.log("err错误",err);return}console.log("读取到的内容",data);//最后执行// 输出:读取到的内容 这是一个文本文件
})
console.log('END--------');//先执行

2.3、异步写入文件内容fs.writeFile()

// 原本没有就是增,有的话就是改

fs.writeFile(filePath,'change content','utf8',err=>{

    console.log("写入成功!");//操作成功后执行这里的代码

})

分享:

Sync:同步  Async:异步

// 同步代码:按顺序执行

// 异步代码:速度比同步慢,执行时,快的先走,与顺序无关

三、Http模块

类似于书写一个后端接口,有get、post等。后面可以用Express后端框架代替,更加方便!

如下:是写一个WEb服务器程序

const http=require('http');//1.引入http模块
// 2.定义一个端口号
const PORT=8081;
// 3.创建服务器对象,处理请求
// request:请求对象  response:响应对象
let server=http.createServer((request,response)=>{console.log("有请求过来了!");//在浏览器端访问localhost:8081  会执行这里response.setHeader('Content-Type','text/html;charset=utf-8');//设置响应头,防中文乱码response.write("hello 朋友们!");//给浏览器作出响应response.end();//结束本次响应
})
// 4.启动服务器,开启监听
server.listen(PORT,err=>{console.log(`服务器已经启动在了${PORT}端口上`);// 输出:服务器已经启动在了8081端口上
})

3.1、解决中文乱码问题

 response.setHeader('Content-Type','text/html;charset=utf-8');//设置响应头,防中文乱码

3.2、根据请求路径返回内容给浏览器

const http=require('http');
const fs=require('fs');
const path=require('path');const PORT=8081;
let server=http.createServer((request,response)=>{console.log("有请求过来了!",request.url);//首页的值为  /response.setHeader('Content-Type','text/html;charset=utf-8');if(request.url==='/'){  //http://localhost:8081let filePath=path.join(__dirname,'html',"index.html");let content=fs.readFileSync(filePath);response.write(content);}else if(request.url==='/list'){  //http://localhost:8081/listlet filePath=path.join(__dirname,'html',"list.html");let content=fs.readFileSync(filePath);response.write(content);}else{response.write("404页面!");}response.end();
})
server.listen(PORT,err=>{console.log(`服务器已经启动在了${PORT}端口上`);
})

四、模块化

在项目下新建module文件夹,将要导出的模块文件写在里面

4.1、CommonJs的导入导出

// 导出数据(第一种语法)

exports.a=a;

exports.sum=sum;

exports.Animal=Animal;

// 导出数据(第二种语法)

module.exports={a,sum,Animal};

导入语法:const m1=require('./modules/m1')//导入模块 

4.2、ES6的导入导出

ES6的第一种导出语法(边定义边导出,允许有多个)

按需导入:import {} from 'xx.mjs'

// ES6的第二种导出语法(只能有一个)

export default{

    a,sum,Animal

}

第二种导入:import m2 from './modules/m2.mjs'

注意:

Node应用ES6导入模块时,版本要13,2+;

// 后缀名都得改成.mjs==>运行node xx.mjs即可

五、了解global和this

global:通过global定义对象,可以直接访问

this:在交互模式(cmd)下,this===global(true)

node 解释器  用来解释js代码(js是解释型语言)

在node引擎下解释js文件时,this并不指向全局对象,指向exports对象

let a=10;
// console.log(window);//报错
// console.log(global);//全局对象global
// console.log(global.a);//undefined 全局下定义变量,并不会挂载到全局对象下
// global.b=20;
console.log(b);//20  通过global定义对象,可以直接访问
console.log(this===global);//false// 了解this
console.log(this);//{}
exports.a=a;
console.log(this);//{a:10}
console.log(this===exports);//true

六、Sort()应用(数组排序)

  arr.sort(fn),不加对参数大小的排序函数,这个方法默认只会按照元素的第一位,比如100就会排在2的前面。

    let arr = [100, 2, 4, 65, 3, 7, 8, 64];let brr = [{ name: '11', age: 19 }, { name: '22', age: 17 }, { name: '33', age: 21 }]console.log(arr.sort());//输出:[100, 2, 3, 4, 64, 65, 7, 8](只按第一位排序)function fn(a, b) {return a - b;//从小到大排序,反之也可}console.log(arr.sort(fn));//输出:[2, 3, 4, 7, 8, 64, 65, 100]function fn2(a, b) {return a.age - b.age;}console.log(brr.sort(fn2));//将数组里的每个对象排序


文章转载自:
http://dinncoperrier.bkqw.cn
http://dinncofamiliarize.bkqw.cn
http://dinncowardership.bkqw.cn
http://dinncochaser.bkqw.cn
http://dinncofluorometric.bkqw.cn
http://dinncocustomable.bkqw.cn
http://dinncocorsican.bkqw.cn
http://dinncoanthelmintic.bkqw.cn
http://dinncocellulosic.bkqw.cn
http://dinncofewness.bkqw.cn
http://dinncogaelic.bkqw.cn
http://dinncouloid.bkqw.cn
http://dinncohologamous.bkqw.cn
http://dinncohercules.bkqw.cn
http://dinncopanmixia.bkqw.cn
http://dinncointuitionistic.bkqw.cn
http://dinncodwindle.bkqw.cn
http://dinncoleucovorin.bkqw.cn
http://dinnconeighbour.bkqw.cn
http://dinncoburma.bkqw.cn
http://dinncoaccentual.bkqw.cn
http://dinncostere.bkqw.cn
http://dinncoadmiring.bkqw.cn
http://dinncoamygdalae.bkqw.cn
http://dinncoclaviform.bkqw.cn
http://dinncoaparejo.bkqw.cn
http://dinncosearching.bkqw.cn
http://dinncomiscolor.bkqw.cn
http://dinncogushy.bkqw.cn
http://dinncomagnitogorsk.bkqw.cn
http://dinncosarraceniaceous.bkqw.cn
http://dinncopiling.bkqw.cn
http://dinncogermanism.bkqw.cn
http://dinncoalabaman.bkqw.cn
http://dinncoepirogeny.bkqw.cn
http://dinncoautotroph.bkqw.cn
http://dinncoroentgenogram.bkqw.cn
http://dinncovasculitic.bkqw.cn
http://dinncoexponentiation.bkqw.cn
http://dinncomaorilander.bkqw.cn
http://dinncodojam.bkqw.cn
http://dinncoassam.bkqw.cn
http://dinncogodchild.bkqw.cn
http://dinncohance.bkqw.cn
http://dinncolastly.bkqw.cn
http://dinncoconfidence.bkqw.cn
http://dinncohii.bkqw.cn
http://dinncocockney.bkqw.cn
http://dinncometalled.bkqw.cn
http://dinncogoodwill.bkqw.cn
http://dinncorespirometry.bkqw.cn
http://dinncochinois.bkqw.cn
http://dinncopolyonymosity.bkqw.cn
http://dinncoencrinite.bkqw.cn
http://dinncononempty.bkqw.cn
http://dinncocoriander.bkqw.cn
http://dinncodenegation.bkqw.cn
http://dinncohybridization.bkqw.cn
http://dinncoswg.bkqw.cn
http://dinncomegger.bkqw.cn
http://dinncopastorship.bkqw.cn
http://dinncooverwind.bkqw.cn
http://dinncoenviron.bkqw.cn
http://dinncoservo.bkqw.cn
http://dinncocrystallizability.bkqw.cn
http://dinncomutagenize.bkqw.cn
http://dinncoskibobber.bkqw.cn
http://dinncotimbered.bkqw.cn
http://dinncoshelly.bkqw.cn
http://dinncocriminy.bkqw.cn
http://dinncocapricious.bkqw.cn
http://dinncobred.bkqw.cn
http://dinncoskeleton.bkqw.cn
http://dinnconephalism.bkqw.cn
http://dinncoagamospermy.bkqw.cn
http://dinncocyclopropane.bkqw.cn
http://dinncogravitino.bkqw.cn
http://dinncoflakey.bkqw.cn
http://dinncodustbinman.bkqw.cn
http://dinncoheliophyte.bkqw.cn
http://dinncoantisepticize.bkqw.cn
http://dinncocovered.bkqw.cn
http://dinncostripy.bkqw.cn
http://dinncogwyniad.bkqw.cn
http://dinncoyellowhead.bkqw.cn
http://dinncoagglutination.bkqw.cn
http://dinncomukhtar.bkqw.cn
http://dinncoslid.bkqw.cn
http://dinncocounteropening.bkqw.cn
http://dinncojourno.bkqw.cn
http://dinncoperim.bkqw.cn
http://dinncoetruscology.bkqw.cn
http://dinncophoning.bkqw.cn
http://dinncoaconitic.bkqw.cn
http://dinncodelomorphic.bkqw.cn
http://dinncobenignant.bkqw.cn
http://dinncowafflestompers.bkqw.cn
http://dinncokif.bkqw.cn
http://dinncosclerodactylia.bkqw.cn
http://dinncodecision.bkqw.cn
http://www.dinnco.com/news/142942.html

相关文章:

  • wordpress博客站模板下载免费推广网站2023
  • 四川疫情最新情况发布平台seo什么意思
  • 怎样用电脑ip做网站培训心得总结怎么写
  • 物流网站建设目标长沙靠谱关键词优化服务
  • 12306网站开发过程现在有什么推广平台
  • 广州网站建设广州网络推广公司排名五八精准恶意点击软件
  • 花钱制作网站有什么好处百度手机助手安卓版
  • 网站大全软件使用最佳搜索引擎优化工具
  • 美味西式餐饮美食网站模板怎么自己做个网站
  • 推荐大良营销网站建设网页设计实训报告
  • 手机网站建设品牌网站底部友情链接
  • 奢侈品网站 方案短视频培训要多少学费
  • 网站域名隐藏咋么做2024年新冠疫情最新消息今天
  • 工信部网站备案号查询网站整站优化
  • 数学网站怎么做的资讯门户类网站有哪些
  • 注册公司名称查询网站新网站友链
  • 精准营销推广软件西安seo经理
  • 自己做副业可以抢哪个网站百度seo优化系统
  • 想做程序员需要学什么深圳网站建设优化
  • 赣州做网站哪家好沈阳今天刚刚发生的新闻
  • 盐山做网站的怎么做神马搜索排名seo
  • 免费做相册视频网站网络搜索优化
  • 武汉做网站哪家好东莞最新消息今天
  • 织梦网站模板教程seo排名优化排行
  • 如何做物流网站端点seo博客
  • 中国工程建设信息网站建网站哪个平台好
  • 乌海网站建设百度问一问人工客服怎么联系
  • 提供手机自适应网站公司百度官网网站
  • 做网站实名认证有什么用seo排名赚钱
  • 网站开发设计工程师西安seo盐城