将coffeescript合并到您的节点项目中? [英] Merge coffeescript into your node project?
问题描述
设置一堆监视器来编译js仍然是开发中的最佳方法,还是有更优雅的东西?
Is setting a bunch of watchers to compile to js still the best approach in development or is there something more elegant?
我正在寻找在开发中处理CoffeeScript的技术或软件包,使它非常好,而不仅仅是观看和编译到另一个js文件夹。有什么吗?
I'm looking for techniques or packages that handle CoffeeScript in development and make it really nice instead of just watching and compiling to another js folder. Anything out there?
感谢任何想法!
推荐答案
CoffeeScript中的node.js服务器代码
你的node.js服务器代码,你不需要做任何特别的事情。只需使用 coffee server.coffee
而不是节点server.js
启动应用程序,coffeescript会将您的代码转换为javascript
node.js server code in CoffeeScript
So for writing your node.js server code, you don't need to do anything special. Just start your app with coffee server.coffee
instead of node server.js
and coffeescript will transpile your code to javascript on the fly without ever needing to write .js
files to disk.
用于在磁盘上获取 .coffee
文件并提供函数 .js
文件到浏览器,可以使用 coffee-script
节点模块进行转换并提供输出。手动编码只是几行,但这几行已存在作为连接兼容的中间件。使用 connect-coffee-script 模块。这个链接上有一些基本的例子,但它可以归结为 app.use(connectCoffeeScript({src:#{__ dirname} / public}))
。这与 express 3.x版兼容。当一个 .js
URL的请求进来时,中间件会找到相应的 .coffee
文件, src
到 dest
。你应该有连接静态中间件配置为从您的 dest
目录进一步向中间件链提供文件,它将是连接静态中间件,实际服务器
For taking .coffee
files on disk and serving the transpiled .js
files to the browser, you can use the coffee-script
node module to do the transpiling and serve the output. Coding it manually is just a few lines, but those few lines already exist as connect compatible middleware. Use the connect-coffee-script module. There are basic examples at that link, but it boils down to app.use(connectCoffeeScript({src: "#{__dirname}/public"}))
or some variation thereof. This is compatible with express version 3.x. When a request for a .js
URL comes in, the middleware will locate the corresponding .coffee
file and transpile it from src
to dest
if needed. You should have the connect static middleware configured to serve files from your dest
directory further down your middleware chain and it will be the connect static middleware that actually servers the .js
file to the browser.
这篇关于将coffeescript合并到您的节点项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!