我喜欢在CoffeeScript中编写我的堆栈,我只是想知道如何在运行时使用Node.js自动编译它,所以我不必手动编译我编辑的每个咖啡文件并移动.js进入文件,这就是我现在所拥有的(仅用于测试);
/routes/index.coffee:
exports.index = (req, res) ->
res.render "index",
title: "Express"
app.js:
/**
* Module dependencies.
*/
require('coffee-script');
var express = require('express')
, staticp = require('./routes/index')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
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(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', staticp.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Error:
https://gist.github.com/Gacnt/8f550196813df8fe1d5f
答案 0 :(得分:5)
您可以启动coffee -wc *.coffee
,它将在保存时重新编译.coffee文件。
答案 1 :(得分:4)
事先require('coffee-script');
。
更新:
如果您使用的是CoffeeScript> = 1.7.0,则需要register()
。