我正在尝试使用Coffeescript以下列方式定义一些Node路由:
我的app.js文件:
/**
* Module dependencies.
*/
require('coffee-script');
var express = require('express');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
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());
}
require('./routes')(app);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我的routes.coffee文件(位于与应用程序相同的级别):
routes = (app) ->
app.get '/login', (req,res) ->
res.render "views/login",
title: 'Login'
stylesheet: 'login'
module.exports = routes
我的问题是:
1当我尝试运行节点应用程序时 - 我得到:
module.js:340
throw err;
^
Error: Cannot find module './routes'
如果我明确指定.coffee后缀:require('./ routes.coffee')(app); - 我明白了:
routes = (app) ->
^
SyntaxError: Unexpected token >
请问这样做的正确方法是什么?
答案 0 :(得分:15)
在CoffeeScript 1.7中,行require('coffee-script')
不再允许您需要CoffeeScript文件。相反,您需要:
require('coffee-script/register')
或
require('coffee-script').register()