0

我是 nodejs 的新手以及与之相关的一切。我正在使用带有节点 js 的 twitter-bootstrap 框架。我运行了以下命令 -

npm install -g twitter-bootstrap-node
twitter-bootstrap create myproject

默认情况下这是使用 express 2,所以我升级到 express 3。从那时起,当我开始 express 时出现以下错误-

$ node app
Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:

  var express = require("express");
  var app = express();


/Users/tusharmathur/Pebbles/app.js:17
  app.use(express.compiler({
                  ^
TypeError: Object function createApplication() {
  var app = connect();
  utils.merge(app, proto);
  app.request = { __proto__: req };
  app.response = { __proto__: res };
  app.init();
  return app;
} has no method 'compiler'
    at Function.app.configure.app.use.express.errorHandler.dumpExceptions (/Users/tusharmathur/Pebbles/app.js:17:19)
    at Function.app.configure (/Users/tusharmathur/Pebbles/node_modules/express/lib/application.js:395:61)
    at Object.<anonymous> (/Users/tusharmathur/Pebbles/app.js:12:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:903:3

这是我的 app.js 文件

/**
 * Module dependencies.
 */

var express = require('express'),
  routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.compiler({
    src: __dirname + '/public',
    enable: ['less']
  }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));

});

app.configure('development', function() {
  app.use(express.errorHandler({
    dumpExceptions: true,
    showStack: true
  }));
});

app.configure('production', function() {
  app.use(express.errorHandler());
});

// Compatible

// Now less files with @import 'whatever.less' will work(https://github.com/senchalabs/connect/pull/174)
var TWITTER_BOOTSTRAP_PATH = './vendor/twitter/bootstrap/less';
express.compiler.compilers.less.compile = function(str, fn) {
  try {
    var less = require('less');
    var parser = new less.Parser({
      paths: [TWITTER_BOOTSTRAP_PATH]
    });
    parser.parse(str, function(err, root) {
      fn(err, root.toCSS());
    });
  } catch (err) {
    fn(err);
  }
}

// Routes

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

app.listen(3000, function() {
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
4

1 回答 1

1

正如消息所说,Express 不再具有此编译器消息。您应该使用另一个允许您构建 Less 文件的中间件。一个例子是less.js-middleware

安装它 ( npm install less-middleware) 后,您可以按照文档中的说明使用它:

var lessMiddleware = require('less-middleware');    
var app = express.createServer();

app.configure(function () {    
    app.use(lessMiddleware({
        src: __dirname + '/public',
        compress: true
    }));
});
于 2013-04-05T14:40:42.810 回答