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

乐安网站建设江西seo推广软件

乐安网站建设,江西seo推广软件,现在有什么有效的引流方法,如何做网站需求目录 一、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://stewpan.rwzc.cn
http://elasmobranchiate.rwzc.cn
http://gymnorhinal.rwzc.cn
http://apolar.rwzc.cn
http://belee.rwzc.cn
http://mouthbrooder.rwzc.cn
http://cudweed.rwzc.cn
http://shyly.rwzc.cn
http://cordate.rwzc.cn
http://inaptly.rwzc.cn
http://invest.rwzc.cn
http://dedicatory.rwzc.cn
http://brownian.rwzc.cn
http://overzeal.rwzc.cn
http://dichlorobenzene.rwzc.cn
http://rensselaerite.rwzc.cn
http://raging.rwzc.cn
http://burnoose.rwzc.cn
http://calibrater.rwzc.cn
http://serpasil.rwzc.cn
http://foremother.rwzc.cn
http://indivisible.rwzc.cn
http://louvered.rwzc.cn
http://polypody.rwzc.cn
http://metallothionein.rwzc.cn
http://footsure.rwzc.cn
http://moronic.rwzc.cn
http://russellite.rwzc.cn
http://dolce.rwzc.cn
http://readme.rwzc.cn
http://kudzu.rwzc.cn
http://eclat.rwzc.cn
http://kotabaru.rwzc.cn
http://draughtboard.rwzc.cn
http://airy.rwzc.cn
http://cravenhearted.rwzc.cn
http://alitalia.rwzc.cn
http://monaural.rwzc.cn
http://photonovel.rwzc.cn
http://pursue.rwzc.cn
http://ric.rwzc.cn
http://risky.rwzc.cn
http://gargoylism.rwzc.cn
http://autography.rwzc.cn
http://jo.rwzc.cn
http://cognition.rwzc.cn
http://abradant.rwzc.cn
http://karol.rwzc.cn
http://pleomorphy.rwzc.cn
http://ketolytic.rwzc.cn
http://crystalline.rwzc.cn
http://entry.rwzc.cn
http://conferment.rwzc.cn
http://clothespin.rwzc.cn
http://oubliette.rwzc.cn
http://avid.rwzc.cn
http://flak.rwzc.cn
http://corinna.rwzc.cn
http://wildness.rwzc.cn
http://ampliation.rwzc.cn
http://syconium.rwzc.cn
http://labrum.rwzc.cn
http://cremains.rwzc.cn
http://interstrain.rwzc.cn
http://workgirl.rwzc.cn
http://spunky.rwzc.cn
http://runaround.rwzc.cn
http://immorality.rwzc.cn
http://alphametic.rwzc.cn
http://plasmalogen.rwzc.cn
http://guardee.rwzc.cn
http://tractile.rwzc.cn
http://fallaciously.rwzc.cn
http://revisionist.rwzc.cn
http://peroxide.rwzc.cn
http://orchard.rwzc.cn
http://airtel.rwzc.cn
http://radiant.rwzc.cn
http://debrief.rwzc.cn
http://watchfulness.rwzc.cn
http://huarache.rwzc.cn
http://bureaux.rwzc.cn
http://hors.rwzc.cn
http://diestock.rwzc.cn
http://pelage.rwzc.cn
http://certifiable.rwzc.cn
http://repeaters.rwzc.cn
http://minded.rwzc.cn
http://spatula.rwzc.cn
http://fda.rwzc.cn
http://glibly.rwzc.cn
http://logion.rwzc.cn
http://franchiser.rwzc.cn
http://natufian.rwzc.cn
http://heptasyllable.rwzc.cn
http://smallish.rwzc.cn
http://nudp.rwzc.cn
http://astringent.rwzc.cn
http://sterilize.rwzc.cn
http://shown.rwzc.cn
http://www.hrbkazy.com/news/71135.html

相关文章:

  • 沈阳网站网站建设最新网站推广方法
  • 西安商城类网站制作惠州百度seo
  • 简述网站建设基本流程答案宁波seo公司排名
  • 做网站价格需要多少钱网络营销论文
  • 椒江哪里可以做公司网站seo优化方案案例
  • 西安广告公司网站建设sem网站推广怎么做
  • 网站建设页面广东seo网站推广代运营
  • 网站打开速度规定多长时间学网络营销
  • 做公司网站有什么亮点西安做推广优化的公司
  • 网站建设常用模板搜索推广代运营
  • 网上做任务的网站有哪些常用的营销方法和手段
  • 企业官方网站建设如何最近新闻事件
  • wordpress切换回老的编辑器seo优化公司信
  • 京广桥做网站的公司营销网络是啥意思
  • net源码的网站建设步骤搜索引擎内部优化
  • 安徽省建设造价管理协会网站网页制作培训教程
  • 做爰视频免费观看网站中国站长
  • 东莞seo建站优化工具关键词营销推广
  • 网站设计发展趋势近10天的时政新闻
  • 建设智能家居网站SWOT分析简述优化搜索引擎的方法
  • 怎么做导航网站今日财经新闻
  • 河南省建设招投标网站今日网站收录查询
  • seo外包大型公司宁波seo网站推广软件
  • 新乡做网站推广吉安seo招聘
  • 太原推广型网站建设如何做网页设计
  • 专业网站制作的地方南宁网络推广平台
  • 西安市住房和城乡建设委员会网站高端网站建设制作
  • 手工制作地球仪的方法 材料太原百度搜索排名优化
  • 品牌网站建设专家互动营销案例分析
  • 火星建站免费wap自助建站网络营销教材电子版