如何将Node和Express与coffeescript和requirejs一起使用?

时间:2012-11-24 19:57:32

标签: node.js coffeescript express requirejs

这就是我想要的。

  • 使用快速网络服务器的节点应用程序
  • 在服务器上使用coffeescript,更重要的是在客户端上使用
  • 在客户端(最终在服务器上)使用require.js

我能够找到为客户提供coffeescript的推荐方法是使用connect-assets。这似乎需要使用jade helper来实际编译coffeescript,例如。

!=js('monalisa.js')

似乎编译monalisa.coffee并生成正确的<script>标记。现在我想使用require.js,在这里我偶然发现。如何确保connect-assets在不使用jade helper的情况下正确编译所有内容?

这是我相当简单的app.js:

require('coffee-script');

var express = require('express')
  , http = require('http')
  , path = require('path')
  , connectAssets = require('connect-assets');

var publicDir = path.join(__dirname, 'public');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');

  app.use(express.favicon());
  app.use(express.logger('dev'));

  app.use(express.bodyParser());
  app.use( connectAssets() );
  app.use('/public', express.static(publicDir));

  app.use(express.logger());
  app.use(express.methodOverride());
  app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
  }));
});

app.get('/', require('./routes').index);
app.get('/monalisa', require('./routes/monalisa').monalisa);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

3 个答案:

答案 0 :(得分:4)

我已经创建了一个包来帮助解决这个问题;它被称为connect-assets-jspaths

自述文件:

安装

npm install connect-assets-jspaths

  • 请注意,依赖于CoffeeScript。

服务器端用法

assets = require "connect-assets"
jsPaths = require "connect-assets-jspaths"

# Snip ...

app.use assets()
# Exports the global function exportPaths() and jsUrl(); see below in View Helpers.
jsPaths assets

# Optionally, pass a log function to see progress
# jsPaths assets, console.log

观察更改并重新编译

现在您可以传递一些额外的回调,它将监视您的连接资产目录以进行更改。

fileChangedCallback = (err, filePath) ->
    console.log "File Changed: #{filePath}"

jsPaths assets, console.log, fileChangedCallback, (err, watcher) ->
    console.log "Watcher initialized"

注意您可能希望在生产模式下禁用此功能。

查看用法

此模块导出两个全局函数exportPaths()jsUrl()

// Using this in your view
!= exportPaths("jsPaths")

// Turns into this when rendered in production
<script type="text/javascript">
    var jsPaths = { "main", "/builtAssets/js/main.13819282742.js" /* snip all the other file paths */ };
</script>


// Using this in your view
- var mainJsPath = jsUrl("/js/main.js")
script(type="text/javascript", data-main="#{mainJsPath}", src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.0.2/require.min.js")    

// Turns into this when rendered in production
<script type="text/javascript" data-main="/builtAssets/js/main.13819282742.js" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.0.2/require.min.js"></script>

动态需求路径

现在我们有一个带有requireJS友好路径的变量,我们可以在RequireJS配置中设置这些路径

# Example main.coffee file in /assets/js folder

requirePaths =
  paths:
    jquery: "//cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min"
    underscore: "//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min"
    backbone: "//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min"
    text: "/js/lib/text"
    handlebars: "/js/lib/handlebars"

if jsPaths
  for own key, value of jsPaths
    # Fix up the lib references
    key = key.slice 4 if key.slice(0, 4) == "lib/"
    requirePaths.paths[key] = value 

require.config
  paths: requirePaths.paths

  shim:
    jquery:
      exports: "$"
    underscore:
      exports: "_"
    backbone:
      deps: ["underscore", "jquery"]
      exports: "Backbone"

require ['app'], (App) ->
    new App().initialize()

答案 1 :(得分:2)

尝试含羞草,它会帮助你解决所有这些问题。 http://www.mimosajs.com

mimosa new [name]将为您提供一个包含所有项目的入门项目。

答案 2 :(得分:1)

对不起新答案,但我决定去开帐户。 =)

如果您选择Express作为mimosa new工作流程的一部分,Mimosa将为您提供一个小型Express应用程序。如果您选择CoffeeScript,它会在CoffeeScript中为您提供Express应用。并且它将在支架应用程序中包含RequireJS。所以你不应该重写任何东西。你只需要插入你的东西。如果它给你的Express应用程序的任何东西将作为一个例子让你自己做,而不使用含羞草。