如何让Stylus在CoffeeScript中使用Express和Connect

时间:2011-06-13 18:11:06

标签: node.js connect coffeescript express stylus

我的app.coffee看起来像这样:

connect = require 'connect'
express = require 'express'
jade = require 'jade'
stylus = require 'stylus'

app = express.createServer()

# CONFIGURATION

app.configure(() ->
  app.set 'view engine', 'jade'
  app.set 'views', "#{__dirname}/views"

  app.use connect.bodyParser()
  app.use connect.static(__dirname + '/public')
  app.use express.cookieParser()
  app.use express.session({secret : "shhhhhhhhhhhhhh!"})
  app.use express.logger()
  app.use express.methodOverride()
  app.use app.router

  app.use stylus.middleware({
    force: true
    src: "#{__dirname}/views"
    dest: "#{__dirname}/public/css"
    compress: true
  })
)

# ROUTES

app.get '/', (req, res) ->
  res.render 'index',
    locals:
      title: 'Title'

# SERVER

app.listen(1234)
console.log "Express server listening on port #{app.address().port}"

更新:我根本不会写CSS文件。

2 个答案:

答案 0 :(得分:4)

找到答案,补充道:

compile = (str, path, fn) ->
  stylus(str).set('filename', path).set('compress', true)

答案 1 :(得分:3)

您当然可以提供自己的编译功能,但它会不必要地覆盖默认功能。相反,将调试选项添加到中间件调用并检查出错的地方:

  app.use stylus.middleware
    debug: true
    force: true
    src: "#{__dirname}/../public"
    dest: "#{__dirname}/../public"

对我来说,问题是设置了错误的src / dest路径。您确定您的.styl文件确实位于您的视图文件夹中吗?