We'll read a csv file in node.js both synchronously, and asynchronously. The file we're reading is a plain text, utf8 file - but you can also use fs.readFile to read a binary file as a buffer. We'll look at the differences between readFile and readFileSync, and show examples of how to catch errors if they occur.

const fs = require('fs')

// Async:

fs.readFile('data.csv', 'utf8', (err, data) => {
console.log(data)
}) // Sync: let results try {
// (invalid file error example)
const data = fs.readFileSync('nofile.csv', 'utf8')
results = data
} catch(e) {
console.log("error", e)
} console.log("results", results)

[Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync的更多相关文章

  1. [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync

    In node.js, you can require fs, and then call fs.writeFile with the filename, and data to write to t ...

  2. node.js delete directory & file system

    node.js delete directory & file system delete a not empty directory https://nodejs.org/api/fs.ht ...

  3. [Whole Web, Node.js, PM2] Configuring PM2 for Node applications

    In this lesson, you will learn how to configure node apps using pm2 and a json config file. Let's sa ...

  4. node.js fs.open 和 fs.write 读取文件和改写文件

    Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...

  5. 《Node.js高级编程》之Node 核心API基础

    Node 核心API基础 第三章 加载模块 第四章 应用缓冲区 第五章 事件发射器模式简化事件绑定 第六章 使用定时器制定函数执行计划 第三章 加载模块 本章提要 加载模块 创建模块 使用node_m ...

  6. 《Node入门》读书笔记——用Node.js开发一个小应用

    Android APP的开发告一段落,一个稳定的.实现了基本功能的APP已经交付用户使用了!我和老板交流了下,接下来准备转战Node.js了,而且一部分前端的功能也要做进去!哈哈哈~~~接下来要朝一个 ...

  7. 深入浅出Node.js(一) - 初识Node.js

    1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...

  8. node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法

    1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...

  9. Edge.js:让.NET和Node.js代码比翼齐飞

    通过Edge.js项目,你可以在一个进程中同时运行Node.js和.NET代码.在本文中,我将会论述这个项目背后的动机,并描述Edge.js提供的基本机制.随后将探讨一些Edge.js应用场景,它在这 ...

随机推荐

  1. nginx 的编译安装及基本操作

    下载nginx [root@nginx ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2019-05-02 21:52:23-- h ...

  2. Vickers Vane Pump Tips - Vane Pump Maintenance Note

    The    Vickers Vane Pump    describes the matters needing attention in the maintenance of the vane p ...

  3. sql中group by

    某图书馆收藏有书籍具有不同的出版年份,管理员需要做一下统计工作: (1)每一年书籍的数目,如: 2000年有10本书, 2001年有5本书... (2)每一种书籍的数目,如: 西游记有10本, 三国演 ...

  4. 题解 洛谷P3622/BZOJ1151【[APIO2007]动物园】

    这一道题,我也是搞了很久才搞懂的(也就两个多小时). 感谢Rayment大佬的题解! 我们进入正题. 对于一个笼子里的动物,我们可以选择撤走或不撤走,可以用0和1来表示,很容易就想到二进制,想到状压d ...

  5. 智能指针unqiue_ptr

    unique_ptr unique_ptr 对它指向的对象在同一时刻是独占的.它要么在构造的时候使用内置指针初始化,要么使用reset给其赋值.当unique_ptr被销毁时,它所指向的对象也被销毁. ...

  6. js事件 (包含call()方法使用特点)

    1.焦点事件 获取焦点事件onfocus\失去焦点事件onblur 例: oText.onfocus=function(){} 焦点:使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么它就可 ...

  7. perl学习之:package and module

    perl的包(package)和模块(PM) ==================================包package===========================     pac ...

  8. Django关于SQL注意事项

    执行原生SQL: from django.db import connection, connections cursor = connection.cursor() cursor.execute( ...

  9. 洛谷 1196 [NOI2002]银河英雄传说【模板】带权并查集

    [题解] 经典的带权并查集题目. 设cnt[i]表示i前面的点的数量,siz[i]表示第i个点(这个点是代表元)所处的联通块的大小:合并的时候更新siz.旧的代表元的cnt,路径压缩的时候维护cnt即 ...

  10. POJ 1252 Euro Efficiency(最短路 完全背包)

    题意: 给定6个硬币的币值, 问组成1~100这些数最少要几个硬币, 比如给定1 2 5 10 20 50, 组成40 可以是 20 + 20, 也可以是 50 -10, 最少硬币是2个. 分析: 这 ...