Skip to content
鼓励作者:欢迎打赏犒劳

06-node读取文件

fs读取txt文件

axios 默认用于发起 HTTP 请求,无法直接读取本地文件。应改用 Node.js 的 fs 模块。

js
import fs from 'fs/promises'; // 使用 Promise 版本的 fs

// 读取模板文件
const loadTemplate = async (path) => {
    const data = await fs.readFile(path, 'utf-8');
    return data;
};

// 添加一个 async 主函数
const main = async () => {
    let data = await loadTemplate('E:\\ws\\demo\\List.txt');
    console.log(data);
};

main()

如有转载或 CV 的请标注本站原文地址