尝试在expressJS应用程序中使用haml咖啡时出现奇怪错误

Strange errors trying to use haml-coffee with an expressJS app

本文关键字:咖啡 错误 haml expressJS 应用程序      更新时间:2023-09-26

这是我的app.js文件,遵循haml咖啡指南:

// 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 'substring' of undefined错误。