我可以用CoffeeScript取代JS来代替node.js吗?

时间:2021-12-17 18:59:46

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?

如果我想要编码节点,我的限制是什么?js和使用CoffeeScript吗?我能用JS做什么?

8 个解决方案

#1


174  

Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.

是的,CoffeeScript只是简单地编译成纯JS,完全兼容node.js。

To run CoffeeScripts on node, you can either:

如要在网上运行coffees,你可以:

  • Type coffee -c example.coffee to compile, followed by node example.js to run the compiled JS.
  • 咖啡- c型的例子。要编译coffee,后面是节点示例。运行编译后的js。
  • Simply type coffee example.coffee
  • 只需输入咖啡example.coffee

#2


88  

Not only can you run CoffeeScript files directly in Node with

不仅可以在节点中直接运行CoffeeScript文件

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

您还可以将它们当作JavaScript文件来使用。例如,如果目录中有lib.coffee,则可以编写

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.

来自同一目录中的另一个CoffeeScript文件。(为了从JavaScript文件中实现这一点,您必须在顶部添加“咖啡脚本”。)因此,您不必在Node下显式地进行编译,除非您正在使用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.

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

#3


56  

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.

    npm安装咖啡吧脚本——保存#我想你已经这样做了。

  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'))

    要求('coffee-script')作为在服务器中执行的第一行。js app.js。(更新:由于coffee脚本1.7,您将需要('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 !).

这将向您的应用程序注册coffecript编译器,您现在可以开始平等地处理coffee文件和js文件(这意味着您也可以要求coffee文件!)

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

这个方法只需要用普通的javascript编写一个文件(app.js)。但是好处是,您的部署环境不需要将coffeescript作为一个初始的全局安装依赖项来运行应用程序。而npm start会让你站起来跑步

#4


15  

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.

我看过佩德罗·特谢拉的优秀教程系列。他一直在构建一个关于节点教程的完整系列。他包括nodemon参考,用于自动检测、编译和重新加载编辑过的.coffee文件。

  1. Coffeescript and Node.js
  2. Coffeescript和node . js
  3. Nodemon
  4. Nodemon

#5


11  

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

您可以使用Jitter,这是对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:

假设你有一堆*。咖啡目录中的咖啡文件,并希望将它们编译到js目录。然后运行:

jitter coffee js

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

抖动在后台运行,直到你终止它(Ctrl+C),观察新的变化。

#6


8  

Coffeescript + ExpressJS + Couchdb + Redis + Auth:

coffecript + ExpressJS + Couchdb + Redis + Auth:

https://gist.github.com/652819

https://gist.github.com/652819

#7


6  

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

CoffeeScript与node有很好的集成。一旦“咖啡脚本”模块被加载,要么通过require(“咖啡脚本”),要么通过上面的she-bang I演示,要么通过运行coffee demo。咖啡……一旦载入,你可以使用require('./foo')来输入foo.coffee

#8


0  

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:

如果您想要自动编译所有coffeescript文件(在一个目录中,包括subdir),每次它们转换为javascript时,请使用以下命令:

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

#1


174  

Yes, CoffeeScript simply compiles into pure JS, making it completely compatible with node.js.

是的,CoffeeScript只是简单地编译成纯JS,完全兼容node.js。

To run CoffeeScripts on node, you can either:

如要在网上运行coffees,你可以:

  • Type coffee -c example.coffee to compile, followed by node example.js to run the compiled JS.
  • 咖啡- c型的例子。要编译coffee,后面是节点示例。运行编译后的js。
  • Simply type coffee example.coffee
  • 只需输入咖啡example.coffee

#2


88  

Not only can you run CoffeeScript files directly in Node with

不仅可以在节点中直接运行CoffeeScript文件

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

您还可以将它们当作JavaScript文件来使用。例如,如果目录中有lib.coffee,则可以编写

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.

来自同一目录中的另一个CoffeeScript文件。(为了从JavaScript文件中实现这一点,您必须在顶部添加“咖啡脚本”。)因此,您不必在Node下显式地进行编译,除非您正在使用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.

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

#3


56  

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.

    npm安装咖啡吧脚本——保存#我想你已经这样做了。

  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'))

    要求('coffee-script')作为在服务器中执行的第一行。js app.js。(更新:由于coffee脚本1.7,您将需要('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 !).

这将向您的应用程序注册coffecript编译器,您现在可以开始平等地处理coffee文件和js文件(这意味着您也可以要求coffee文件!)

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

这个方法只需要用普通的javascript编写一个文件(app.js)。但是好处是,您的部署环境不需要将coffeescript作为一个初始的全局安装依赖项来运行应用程序。而npm start会让你站起来跑步

#4


15  

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.

我看过佩德罗·特谢拉的优秀教程系列。他一直在构建一个关于节点教程的完整系列。他包括nodemon参考,用于自动检测、编译和重新加载编辑过的.coffee文件。

  1. Coffeescript and Node.js
  2. Coffeescript和node . js
  3. Nodemon
  4. Nodemon

#5


11  

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

您可以使用Jitter,这是对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:

假设你有一堆*。咖啡目录中的咖啡文件,并希望将它们编译到js目录。然后运行:

jitter coffee js

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

抖动在后台运行,直到你终止它(Ctrl+C),观察新的变化。

#6


8  

Coffeescript + ExpressJS + Couchdb + Redis + Auth:

coffecript + ExpressJS + Couchdb + Redis + Auth:

https://gist.github.com/652819

https://gist.github.com/652819

#7


6  

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

CoffeeScript与node有很好的集成。一旦“咖啡脚本”模块被加载,要么通过require(“咖啡脚本”),要么通过上面的she-bang I演示,要么通过运行coffee demo。咖啡……一旦载入,你可以使用require('./foo')来输入foo.coffee

#8


0  

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:

如果您想要自动编译所有coffeescript文件(在一个目录中,包括subdir),每次它们转换为javascript时,请使用以下命令:

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