简体   繁体   中英

How do I auto-compile .coffeescript with Node.js?

I enjoy writing my stack in CoffeeScript, I'm just wondering how I can get it to auto compile at run time with Node.js so I don't have to manually compile every coffee file I edit and move the .js into the file, this is what I have right now (just for a test);


/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

你可以启动coffee -wc *.coffee ,它会在保存时重新编译.coffee文件。

Just require('coffee-script'); beforehand.

UPDATE : If you're using CoffeeScript >=1.7.0, you need to register() it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM