nodejs path使用path模块处理路径
taotaoit nodejs 2023-01-10 751 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6505747 2619 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)
path 模块是 Node.js 官方提供的、用来处理路径的模块。它提供了一系列的方法和属性,用来满足用户对路径的处理需求。

1,路径拼接 path.join()

const path = require('path')
const fs = require('fs')

// 注意:../会抵消前面的路径
// ./会被忽略
const pathStr = path.join('/a','/b/c','../','./d','e')
console.log(pathStr); // \a\b\d\e

// 注意:今后凡是涉及到路径拼接的操作,都要使用path.join()方法进行处理,不要使用+进行字符串拼接
// const pathStr2 = path.join(__dirname,'./files/1.txt')
// console.log(pathStr2);
fs.readFile(path.join(__dirname,'./files/1.txt'),'utf8',function(err,data){
  if(err){
    return console.log('读取错误' + err.message);
  }
  console.log('读取成功');
})

2,获取路径中文件名 path.basename() 

使用 path.basename() 方法,可以获取路径中的最后一部分,常通过该方法获取路径中的文件名

path.basename(path[, ext])
path: 文件路径
ext: 文件扩展名

const path = require('path')

// 定义文件的存放路径
const fpath = '/a/b/c/index.html'

// 获取完整文件名
const fullName = path.basename(fpath)
console.log(fullName); // index.html

// 获取没有扩展名的文件名
const nameWithoutExt = path.basename(fpath,'.html')
console.log(nameWithoutExt); // index
3,获取路径中文件扩展名 path.extname()

const path = require('path')

const fpath = '/a/b/c/index.html'

const fext = path.extname(fpath)
console.log(fext) // .html

版权声明:本站原创文章,允许自由转载。

相关推荐
windows如何把已安装的nodejs高版本降级为低版本
nodejs | 2022-10-17 2089
第一步:先清空本地安装的node.js版本 1.按健win+R弹出窗口,键盘输入cmd,然后敲回车,然后进入命令控制行窗口,并输入where node查看之前本地安装的node的路径 2.找到上面找到的路径,将node.exe所在的父目录里面的所有东西都删除 3.为了彻底删除之前安装的node.js,鼠...
This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher.
nodejs | 2022-07-06 1811
有的nodejs版本不支持win7,在win7系统中执行npm -v时会有以下提示 This application is only supported on Windows 8.1, Windows Server 2012 R2, or hi gher. 下载v12.16.2及之前的版本即可 https://nodejs.org/d...
评论:0条
评论加载中...
发表评论
后一篇: