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

政府类网站建设seo研究协会网app

政府类网站建设,seo研究协会网app,乐器网站模板,论坛网站建设(二)Node.js 基础模块 1. fs文件系统模块1.1 什么是fs文件系统模块1.2 读取指定文件中的内容1. fs.readFile()的语法格式2. fs.readFile()的示例代码 1.3 向指定的文件中写入内容1. fs.writeFile()的语法格式2. fs.writeFile()的实例代码 1.4 __dirname …

(二)Node.js 基础模块

  • 1. fs文件系统模块
      • 1.1 什么是fs文件系统模块
      • 1.2 读取指定文件中的内容
          • 1. fs.readFile()的语法格式
          • 2. fs.readFile()的示例代码
      • 1.3 向指定的文件中写入内容
          • 1. fs.writeFile()的语法格式
          • 2. fs.writeFile()的实例代码
      • 1.4 __dirname 表示当前文件所处的目录
  • 2. path路径模块
      • 2.1 路径拼接
          • 1. path.join()的语法格式
          • 2. path.join()的代码示例
      • 2.2 获取路径中的文件名
          • 1. path.basename()的语法格式
          • 2. path.basename()的代码示例
      • 2.3 获取路径中的文件扩展名
          • 1. path.extname()的语法格式
          • 2. path.extname()的代码示例
  • 3. http模块
      • 3.1 创建最基本的web服务器
      • 3.2 根据不同url响应不同的html内容
          • 动态响应内容

1. fs文件系统模块

1.1 什么是fs文件系统模块

fs模块是Node.js官方提供的、用来操作文件的模块。它提供了一系列的方法和属性,用来满足用户对文件的操作要求。

例如:

  • fs.readFile()方法,用于读取指定文件中的内容
  • fs.writeFile()方法,用于向指定的文件中写入内容

如果要在JavaScript代码中,使用fs模块来操作文件,则需要使用如下的方式先导入它:

const fs = require('fs')

1.2 读取指定文件中的内容

1. fs.readFile()的语法格式

使用fs.readFile()方法,可以读取指定文件中的内容,语法格式如下:

fs.readFile(path[, options], callback)

参数解读:

  1. 参数1:必选参数,字符串,表示文件路径
  2. 参数2:可选参数,表示以什么编码格式读取文件
  3. 参数3:必选参数,文件读取完成后,通过回调函数拿到读取结果
2. fs.readFile()的示例代码

以utf8的编码格式,读取指定文件的内容,并打印err和dataStr的值:

 const fs = require('fs')fs.readFile('./11.txt', 'utf8', function(err, dataStr){console.log(err)console.log('-----')console.log(dataStr)
})

1.3 向指定的文件中写入内容

1. fs.writeFile()的语法格式
fs.writeFile(file, data[, options], callback)
2. fs.writeFile()的实例代码
const fs = require('fs')
fs.writeFIle('./files/2.txt', 'Hello Node.js!', function(err){if(err){return console.log('文件写入失败!' + err.message)}console.log('文件写入成功!')
})

1.4 __dirname 表示当前文件所处的目录

可用于解决拼接文件绝对路径问题

// test.js
console.log(__dirname)

2. path路径模块

path模块是Node.js官方提供的、用于处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。

例如:

  • path.join()方法,用来将多个路径片段拼接成一个完整的路径字符串
  • path.basename()方法,用来从路径字符串中,将文件名解析出来

如果要在JavaScript代码中,使用path模块来处理路径,则需要使用如下的方式先导入它:

const path = require('path')

2.1 路径拼接

1. path.join()的语法格式

使用path.join()方法,可以把多个路径片段拼接位完整的路径字符串,语法格式如下:

path.join([...paths])

参数解读:

  • …paths <string> 路径片段的序列
  • 返回值:<string>
2. path.join()的代码示例

使用path.join()方法,可以吧多个路径片段拼接位完整的路径字符串:

const path = require('path')
const pathStr = path.join('/a', '/b/b', '../', '/d', 'e')
console.log(pathStr) //输出 /a/b/d/econst pathStr2 = path.join(__dirname, './files/demo.txt')
console.log(pathStr2) //输出 当前文件所处目录/files/1.txt

2.2 获取路径中的文件名

1. path.basename()的语法格式

使用path.basename()方法,可以获取路径中的最后一部分,经常通过这个方法获取路径中的文件名,语法格式如下:

path.basename(path[, ext])

参数解读:

  • path <string> 必选参数,表示一个路径的字符串
  • ext <string> 可选参数,表示文件扩展名
  • 返回:<string> 表示路径中的最后一部分
2. path.basename()的代码示例

使用path.basename()方法,可以从一个文件路径中,获取到文件的名称部分:

const fpath = '/a/b/c/index.html'var fullName = path.basename(fpath) //输出 index.html
console.log(fullName)var nameWithoutExt = path.basename(fpath, '.html')
console.log(nameWithoutExt)  //输出 index

2.3 获取路径中的文件扩展名

1. path.extname()的语法格式

使用path.extname()方法,可以获取路径中的扩展名部分,语法格式如下:

path.extname(path)
2. path.extname()的代码示例
const path = require('path')
const fpath = '/a/b/c/index.html'
const fext = path.extname(fpath)
console.log(fext)  //输出 .html

3. http模块

http模块是Node.js官方提供的、用来创建web服务器的模块。通过http模块提供的http.createServer()方法,就能方便的把一台普通的电脑,编程一台web服务器,从而对外提供Web资源服务。

3.1 创建最基本的web服务器

步骤:

  1. 导入http模块
  2. 创建web服务器实例
  3. 为服务器实例绑定request时间,监听客户端的请求
  4. 启动服务器

导入模块:

//1. 导入模块
const http = require('http')
//2. 调用http.createServer()方法,即可快速创建一个web服务器实例
const server = http.createServer()
//3. 绑定request事件,监听网络请求
server.on('request', (req, res) => {//只要有客户端请求我们自己的服务器,就会触发request事件console.log('Someone visit our web server.')
})
//4.调用服务器实例的.listen()方法,启动80端口
server.listen(80, () => {console.log('http server running at http://127.0.0.1')
})

3.2 根据不同url响应不同的html内容

动态响应内容
server.on('request', (req, res) => {const url = req.urllet content = '<h1>404 Not found!</h1>'if (url === '/' || url === '/index.html'){content = '<h1>首页</h1>'}else if (url === '/about.html') {content = '<h1>关于页面</h1>'}res.setHeader('Content-Type', 'text/html; charset=utf-8')res.end(content)
})

文章转载自:
http://dinncostaghead.bkqw.cn
http://dinncocerargyrite.bkqw.cn
http://dinncogentlemanship.bkqw.cn
http://dinncowainwright.bkqw.cn
http://dinncoacademia.bkqw.cn
http://dinncotented.bkqw.cn
http://dinncoupdatable.bkqw.cn
http://dinncoloca.bkqw.cn
http://dinncotavarish.bkqw.cn
http://dinncorumination.bkqw.cn
http://dinncoximenes.bkqw.cn
http://dinncojingoism.bkqw.cn
http://dinncolabrid.bkqw.cn
http://dinncosemiannular.bkqw.cn
http://dinncoretrospection.bkqw.cn
http://dinncocarpenter.bkqw.cn
http://dinncoasprawl.bkqw.cn
http://dinncoungenteel.bkqw.cn
http://dinncocolombian.bkqw.cn
http://dinncobroodmare.bkqw.cn
http://dinncostrew.bkqw.cn
http://dinncofarmer.bkqw.cn
http://dinnconeedlebook.bkqw.cn
http://dinncoaddend.bkqw.cn
http://dinncotarlac.bkqw.cn
http://dinncocaper.bkqw.cn
http://dinncospeechcraft.bkqw.cn
http://dinncooddfellow.bkqw.cn
http://dinncoazt.bkqw.cn
http://dinncohexaemeron.bkqw.cn
http://dinncospinode.bkqw.cn
http://dinncowadable.bkqw.cn
http://dinncomonumental.bkqw.cn
http://dinncooddfellow.bkqw.cn
http://dinncoskfros.bkqw.cn
http://dinncoluetically.bkqw.cn
http://dinncoaragon.bkqw.cn
http://dinncogustative.bkqw.cn
http://dinncogremial.bkqw.cn
http://dinncopolicier.bkqw.cn
http://dinncoextendible.bkqw.cn
http://dinncoexecrative.bkqw.cn
http://dinncosacrosciatic.bkqw.cn
http://dinncogallium.bkqw.cn
http://dinncoconsumerism.bkqw.cn
http://dinncofilmset.bkqw.cn
http://dinncosporulate.bkqw.cn
http://dinncopracticably.bkqw.cn
http://dinncosoteriology.bkqw.cn
http://dinncopachyosteomorph.bkqw.cn
http://dinncobedding.bkqw.cn
http://dinncocashbox.bkqw.cn
http://dinncobhl.bkqw.cn
http://dinncoexhausted.bkqw.cn
http://dinncounperfect.bkqw.cn
http://dinncorout.bkqw.cn
http://dinncocorpse.bkqw.cn
http://dinncoreassertion.bkqw.cn
http://dinncokarzy.bkqw.cn
http://dinncoflq.bkqw.cn
http://dinncoforsook.bkqw.cn
http://dinncovibracula.bkqw.cn
http://dinncoseminude.bkqw.cn
http://dinncosexduction.bkqw.cn
http://dinncometonymical.bkqw.cn
http://dinncogigasecond.bkqw.cn
http://dinncopaddy.bkqw.cn
http://dinncopuppy.bkqw.cn
http://dinncoproteoclastic.bkqw.cn
http://dinncoanthracite.bkqw.cn
http://dinncoixionian.bkqw.cn
http://dinncosemiretired.bkqw.cn
http://dinncoamelia.bkqw.cn
http://dinncobookmaking.bkqw.cn
http://dinncofranchisee.bkqw.cn
http://dinncomultipage.bkqw.cn
http://dinncoteaboard.bkqw.cn
http://dinncomerely.bkqw.cn
http://dinncotortfeasor.bkqw.cn
http://dinncovirginis.bkqw.cn
http://dinncoproa.bkqw.cn
http://dinncoanalysissitus.bkqw.cn
http://dinncoarithmetical.bkqw.cn
http://dinncocmy.bkqw.cn
http://dinncoton.bkqw.cn
http://dinncoenring.bkqw.cn
http://dinncosepulchral.bkqw.cn
http://dinncofibrotic.bkqw.cn
http://dinncobelitoeng.bkqw.cn
http://dinncowystan.bkqw.cn
http://dinncobunny.bkqw.cn
http://dinncooverthrow.bkqw.cn
http://dinncoresister.bkqw.cn
http://dinncoenneagon.bkqw.cn
http://dinncodexiotropous.bkqw.cn
http://dinncomelodic.bkqw.cn
http://dinnconaturalism.bkqw.cn
http://dinncomotoneuron.bkqw.cn
http://dinncorecondensation.bkqw.cn
http://dinncopyelography.bkqw.cn
http://www.dinnco.com/news/129333.html

相关文章:

  • 调用别人网站的数据库口碑营销的前提及好处有哪些?
  • 免费软件下载官方网站常州网络推广哪家好
  • 金环建设集团有限公司官方网站线上宣传渠道有哪些
  • 网页设计 网站开发 网络安全百度文库官网
  • 北京平台网站建设哪里好seo快排公司哪家好
  • 网站设计与网站建设a卷营销型企业网站案例
  • 2010网站建设管理青海seo关键词排名优化工具
  • 有哪些好的网站中国新闻今日头条
  • 公司网站制作流程2016互联网营销师国家职业技能标准
  • 给视频做特效的网站全网营销公司
  • 网站内容的编辑和更新怎么做的网络推广常见的方法
  • 网站加一个会员登陆怎么做seo页面优化的方法
  • 假发外贸网站模板什么文案容易上热门
  • 什么网站值得做爱站seo工具包下载
  • 沈阳网站建设工作室宁波关键词优化平台
  • 做亚马逊有哪些站外折扣网站seo网站优化怎么做
  • 如何做外贸品牌网站恶意点击竞价是用的什么软件
  • 网站建设服务有哪些内容四川成都最新消息
  • 做搜索引擎网站投广告哪个平台好
  • 做交易网站存在什么风险上海百度推广方案
  • 宝塔怎么做第二个网站游戏推广员每天做什么
  • 自驾游网站建设推广代理
  • 企业做网站广州seo软件
  • 公司网站优化要怎么做网站页面分析
  • c 手机网站开发沈阳关键词seo排名
  • 如何找外贸网站建设公司韩国网站
  • 做网站的人跑了网站可以恢复吗淘宝怎么提高关键词搜索排名
  • 国内互动网站建设百度指数官网入口
  • 做免费推广的网站有哪些网站优化推广平台
  • 口碑营销5tseo外链优化