我可以使用 CoffeeScript 而不是 Node.js 的 JS 吗?
如果我想编写 Node.js 代码并使用 CoffeeScript,有哪些限制? 我能做任何我能在 JS 中做的事情吗?
What are my restrictions if I want to code node.js and use CoffeeScript?
Can I do anything I'd be able to do in JS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,CoffeeScript只是编译成纯JS,使其与node.js完全兼容。
要在 Node 上运行 CoffeeScripts,您可以:
coffee -c example.coffee
进行编译,然后输入node example.js
运行编译后的 JS。coffee example.coffee
Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.
To run CoffeeScripts on node, you can either:
coffee -c example.coffee
to compile, followed bynode example.js
to run the compiled JS.coffee example.coffee
您不仅可以直接在 Node 中运行 CoffeeScript 文件,
还可以像 JavaScript 文件一样使用它们。例如,如果目录中有
lib.coffee
,则可以从同一目录中的另一个 CoffeeScript 文件进行写入。 (为了从 JavaScript 文件执行此操作,您必须在顶部添加
require 'coffee-script'
。)因此,您永远不必在 Node 下显式进行编译,除非您“使用 npm 等工具重新打包项目以进行部署。需要注意的是:在堆栈跟踪中,您将看到的行号指的是已编译的 JavaScript,即使您直接运行 CoffeeScript(因此您无权访问 JavaScript)。很多人都在尝试解决这个问题,但这是一个巨大的挑战。
Not only can you run CoffeeScript files directly in Node with
you can also require them as if they were JavaScript files. For instance, if you have
lib.coffee
in a directory, you can writefrom another CoffeeScript file in the same directory. (In order to do this from a JavaScript file, you'll have to add
require 'coffee-script'
at the top.) So, you never have to do compilation explicitly under Node, unless you're packaging your project for deployment with a tool like npm.One caveat: In stack traces, the line numbers you'll see refer to the compiled JavaScript, even when you're running CoffeeScript directly (so you don't have access to the JavaScript). A lot of folks are trying to fix this, but it's a big challenge.
是的,这是一个不同的 &更简单的答案。您需要执行 2 个步骤。
npm install Coffee-script --save # 我假设你已经这样做了
。将
require('coffee-script')
作为将在app.js
的server.js
中执行的第一行。 (更新:自咖啡脚本 1.7 起,您必须执行require('coffee-script/register'))
这会将coffeescript编译器注册到您的应用程序,您可以开始处理现在可以同等地使用咖啡文件和 js 文件(这意味着您也可以需要咖啡文件!)。
此方法将要求您仅用普通 JavaScript 编写一个文件 (app.js)。但优点是您的部署环境不需要将 CoffeeScript 作为初始全局安装的依赖项来运行您的应用程序。在这种情况下,您只需复制代码,
npm install
将安装所有必需的软件包。npm start
会让你启动并运行Yes, here's a different & simpler answer. You need to do 2 steps.
npm install coffee-script --save # I assume you would have done this already
.Have
require('coffee-script')
as the first line that would get executed inserver.js
ofapp.js
. (UPDATE: since coffee script 1.7, you will have to dorequire('coffee-script/register'))
This registers coffeescript compiler to your app and you can start treating coffee files and js files equally now (meaning that you can require coffee files too !).
This method will require you to write just the one file (app.js) in vanilla javascript. But the advantage is that your deploy environment need not have coffeescript as an initial globally installed dependency to run your app. In this case, you would just have to copy over your code, and
npm install
would install all packages necessary. Andnpm start
would have you up and running视频教程
我看过Pedro Teixeira制作的精彩教程系列。他一直在构建有关节点教程的整个系列。他包括对 nodemon 的参考,用于自动检测、编译和重新加载已编辑的 .coffee 文件。
Video Tutorials
I've seen a great tutorial series by Pedro Teixeira. He's been building an entire series on node tutorials. He includes reference to nodemon for auto detection and compilation and reloading of edited .coffee files.
您可以使用 Jitter,这是 CoffeeScript 的简单连续编译。
假设您的coffee目录中有一堆*.coffee文件,并且想要将它们编译到js目录中。然后运行:
Jitter 在后台运行,直到您终止它 (Ctrl+C),观察新的更改。
You can use Jitter, a Simple continuous compilation for CoffeeScript.
Let's say you have a bunch of *.coffee files in the coffee directory, and want to compile them to the js directory. Then run:
Jitter runs in the background until you terminate it (Ctrl+C), watching for new changes.
Coffeescript + ExpressJS + Couchdb + Redis + Auth:
https://gist.github.com/652819
Coffeescript + ExpressJS + Couchdb + Redis + Auth:
https://gist.github.com/652819
试试这个
然后做:
CoffeeScript 与 Node 有着非常牢固的集成。一旦加载了“coffee-script”模块,可以通过
require('coffee-script')
、通过我上面演示的 she-bang,或者通过运行coffee demo.coffee
...加载后,您可以使用require('./foo')
引入foo.coffee
Try this
Then do:
CoffeeScript has pretty solid integration with node. Once the 'coffee-script' module is loaded, either by
require('coffee-script')
, by the she-bang I demo'd above, or by runningcoffee demo.coffee
... once loaded, you can usedrequire('./foo')
to bring infoo.coffee
如果您想在每次更改为 javascript 时自动编译所有的 CoffeeScript 文件(在一个目录中,包括子目录),只需使用以下命令:
If you want to automatically compile all your coffeescript files (in one directory including subdir) every time they change into javascript, just use this command: