path.extname()
是 Node.js 中的一个内置模块,用于获取文件路径中的扩展名。根据[1]中的描述,该方法会返回文件路径中最后一个点(.)字符到末尾之间的内容,如果没有点字符则返回空字符串。下面是一个使用 path.extname()
方法的示例:
const path = require('path');
const file1 = '/user/images/avatar.png';
const file2 = '/user/documents/report';
console.log(path.extname(file1)); // 输出:.png
console.log(path.extname(file2)); // 输出:空字符串 ''