使用Coffeescript读取本地文件

时间:2023-02-09 22:57:21

I'm trying to read in a file with Coffeescript. In the same folder where I enter into the coffee repo, I have a file named hello.txt.

我正在尝试用Coffeescript读取一个文件。在我进入咖啡仓库的同一个文件夹中,我有一个名为hello.txt的文件。

coffee> fs = require 'fs'
coffee> x = fs.readFile "hello.txt"
undefined
coffee> x
undefined

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


8  

You're not passing a callback to readFile to actually read the file. See docs for further information. Generally, nodejs methods are asynchronous because of the asynchronous nature of the platform. For some of them there is a sync version. Indeed, you can read a file with the readFileSync method.

您没有将回调传递给readFile来实际读取文件。有关详细信息,请参阅文档。通常,nodejs方法是异步的,因为平台的异步性质。其中一些是同步版本。实际上,您可以使用readFileSync方法读取文件。

#1


8  

You're not passing a callback to readFile to actually read the file. See docs for further information. Generally, nodejs methods are asynchronous because of the asynchronous nature of the platform. For some of them there is a sync version. Indeed, you can read a file with the readFileSync method.

您没有将回调传递给readFile来实际读取文件。有关详细信息,请参阅文档。通常,nodejs方法是异步的,因为平台的异步性质。其中一些是同步版本。实际上,您可以使用readFileSync方法读取文件。