javascript - 尝试将 haml-coffee 与expressJS 应用程序一起使用时出现奇怪的错误

标签 javascript node.js express substring syntax-error

这是我的 app.js 文件,位于haml-coffee guides之后:

// misc requirements
var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path');

// setup app
var app = express();

// set default engine to haml-coffee
app.engine( 'hamlc', require('haml-coffee'.__express ));

// configure misc stuff
app.configure(function(){
  app.set('port', process.env.PORT || 3000); // set localhost port to 3000
  app.set('views', __dirname + '/views');    // setup views (templates)
  app.set('view engine', 'hamlc');           // use haml-coffee as default templating engine
  app.set('title', 'Learn Some Code');       // set the website title

  app.use(express.favicon());                // favicon handling
  app.use(express.logger('dev'));            // logging

  app.use(express.bodyParser());
  app.use(express.methodOverride());

  app.use(app.router); // use the router
  app.use(express.static(path.join(__dirname, 'public')));
});

// only configure this for development
app.configure('development', function(){
  app.use(express.errorHandler());
});

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

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

但是当我运行 node app.js 时...我收到这个奇怪的错误:

module.js:236
  var start = request.substring(0, 2);
                      ^
TypeError: Cannot call method 'substring' of undefined
    at Function.Module._resolveLookupPaths (module.js:236:23)
    at Function.Module._resolveFilename (module.js:328:31)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/chris/src/learnsomecode/app.js:11:22)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

最佳答案

您的要求有一个轻微的拼写错误。改变

app.engine( 'hamlc', require('haml-coffee'.__express ));

app.engine( 'hamlc', require('haml-coffee').__express );

'haml-coffee'.__express 解析为 undefined,这使得 require(undefined) 抛出 Cannot call method '未定义的子字符串错误。

关于javascript - 尝试将 haml-coffee 与expressJS 应用程序一起使用时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12131306/

相关文章:

javascript - 移除广告内容时显示图片

javascript - 文本输入失去焦点并再次获得焦点后自动触发按键事件

javascript - 哈皮,错误 415 : Unsupported Media Type error

javascript - 如何以编程方式禁用 onclick 按钮功能

javascript - 如何在 react native 中设置对点击通知的 react ?

node.js - 无法建立 Bower 错误隧道套接字,原因=解析错误

node.js - 尝试使用 Node fs.writeFile 写入 json 文件

css - MIME 类型 ('text/html' ) 不是受支持的样式表

node.js - 如何将输入数组保存到嵌套 Mongoose 模式中的子模式?

javascript - 对于node.js、express和handlebars,我应该将前端javascript放在哪里?