最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reading a local file with Coffeescript - Stack Overflow

programmeradmin5浏览0评论

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.

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

What am I doing wrong?

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.

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

What am I doing wrong?

Share Improve this question edited Feb 20, 2012 at 23:47 mu is too short 435k71 gold badges859 silver badges818 bronze badges asked Feb 20, 2012 at 23:20 grauturgrautur 30.5k34 gold badges96 silver badges129 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论