如何在 node.js 中获取文件的扩展名
要在 node.js 中获取文件的扩展名,我们可以使用 path
模块中的 extname()
方法。
const path = require('path');
path.extname('style.css') // .css
path.extname('image.png') // .png
path.extname('prettier.config.js') // .js
extname() 方法
extname()
方法从最后一次出现 .
(句点)字符到路径最后部分的字符串末尾。
如果没有 .
在路径的最后一部分,或者如果路径以 .
它是唯一的。 路径中的字符,extname()
返回一个空字符串。
path.extname('index.'); // .
path.extname('index'); // '' (empty string)
path.extname('.index'); // '' (empty string)
path.extname('.index.html'); // .html
如果路径不是字符串,则 extname()
会抛出 typeerror
。
const path = require('path');
// ❌ typeerror: received type number instead of string
path.extname(123);
// ❌ typeerror: received type boolean instead of string
path.extname(false);
// ❌ typeerror: received url instance instead of string
path.extname(new url('https://example.com/file.txt'));
// ✅ received type of string
path.extname('package.json'); // .json
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
发布时间:2024/02/06 浏览次数:146 分类:编程语言
-
本文将讨论几种使用 powershell 脚本获取文件扩展名的方法。本文还提供了多种技术和示例。
发布时间:2023/12/24 浏览次数:88 分类:python
-
它演示了如何在 python 中获取文件扩展名。本教程将介绍如何在 python 中从文件名中获取文件扩展名。
发布时间:2023/08/25 浏览次数:150 分类:c
-
文件扩展名是指文件名的最后部分,其中包含有关文件中保存的数据的信息。在 c 中,我们可以对包含 c 代码的文件使用 .cpp 或 .cxx 扩展名。
node.js 中的 http 发送 post 请求
发布时间:2023/03/27 浏览次数:456 分类:node.js
-
在本文中,我们将学习如何使用 node.js 使用第三方包发出发送 post 请求。