我可以使用 CoffeeScript 而不是 Node.js 的 JS 吗?

发布于 2024-10-12 06:31:05 字数 67 浏览 2 评论 0原文

如果我想编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情话难免假 2024-10-19 06:31:05

是的,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:

  • Type coffee -c example.coffee to compile, followed by node example.js to run the compiled JS.
  • Simply type coffee example.coffee
我恋#小黄人 2024-10-19 06:31:05

您不仅可以直接在 Node 中运行 CoffeeScript 文件,

coffee source.coffee

还可以像 JavaScript 文件一样使用它们。例如,如果目录中有 lib.coffee,则可以

require './lib'

从同一目录中的另一个 CoffeeScript 文件进行写入。 (为了从 JavaScript 文件执行此操作,您必须在顶部添加 require 'coffee-script'。)因此,您永远不必在 Node 下显式进行编译,除非您“使用 npm 等工具重新打包项目以进行部署。

需要注意的是:在堆栈跟踪中,您将看到的行号指的是已编译的 JavaScript,即使您直接运行 CoffeeScript(因此您无权访问 JavaScript)。很多人都在尝试解决这个问题,但这是一个巨大的挑战。

Not only can you run CoffeeScript files directly in Node with

coffee source.coffee

you can also require them as if they were JavaScript files. For instance, if you have lib.coffee in a directory, you can write

require './lib'

from 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.

白芷 2024-10-19 06:31:05

是的,这是一个不同的 &更简单的答案。您需要执行 2 个步骤。

  1. npm install Coffee-script --save # 我假设你已经这样做了

  2. require('coffee-script') 作为将在 app.jsserver.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.

  1. npm install coffee-script --save # I assume you would have done this already.

  2. Have require('coffee-script') as the first line that would get executed in server.js of app.js. (UPDATE: since coffee script 1.7, you will have to do require('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. And npm start would have you up and running

北城挽邺 2024-10-19 06:31:05

视频教程

我看过Pedro Teixeira制作的精彩教程系列。他一直在构建有关节点教程的整个系列。他包括对 nodemon 的参考,用于自动检测、编译和重新加载已编辑的 .coffee 文件。

  1. Coffeescript 和 Node.js
  2. Nodemon

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.

  1. Coffeescript and Node.js
  2. Nodemon
李不 2024-10-19 06:31:05

您可以使用 Jitter,这是 CoffeeScript 的简单连续编译。

npm install -g jitter

假设您的coffee目录中有一堆*.coffee文件,并且想要将它们编译到js目录中。然后运行:

jitter coffee js

Jitter 在后台运行,直到您终止它 (Ctrl+C),观察新的更改。

You can use Jitter, a Simple continuous compilation for CoffeeScript.

npm install -g jitter

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 coffee js

Jitter runs in the background until you terminate it (Ctrl+C), watching for new changes.

萌酱 2024-10-19 06:31:05

Coffeescript + ExpressJS + Couchdb + Redis + Auth:

https://gist.github.com/652819

Coffeescript + ExpressJS + Couchdb + Redis + Auth:

https://gist.github.com/652819

老街孤人 2024-10-19 06:31:05

试试这个

#!/usr/bin/env coffee
v = 78
console.log "The value of v is '#{v}'"

然后做:

chmod +x demo.coffee
./demo.coffee

CoffeeScript 与 Node 有着非常牢固的集成。一旦加载了“coffee-script”模块,可以通过 require('coffee-script')、通过我上面演示的 she-bang,或者通过运行 coffee demo.coffee ...加载后,您可以使用 require('./foo') 引入 foo.coffee

Try this

#!/usr/bin/env coffee
v = 78
console.log "The value of v is '#{v}'"

Then do:

chmod +x demo.coffee
./demo.coffee

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 running coffee demo.coffee ... once loaded, you can used require('./foo') to bring in foo.coffee

余生一个溪 2024-10-19 06:31:05

如果您想在每次更改为 javascript 时自动编译所有的 CoffeeScript 文件(在一个目录中,包括子目录),只需使用以下命令:

find . -name '*.coffee' -type f -print0 | xargs -0 coffee -wc

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:

find . -name '*.coffee' -type f -print0 | xargs -0 coffee -wc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文