【问题标题】:How to get Stylus to work with Express and Connect in CoffeeScript如何让 Stylus 在 CoffeeScript 中使用 Express 和 Connect
【发布时间】:2011-09-14 03:08:05
【问题描述】:

我的 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 文件。

【问题讨论】:

    标签: node.js connect coffeescript express stylus


    【解决方案1】:

    找到答案,补充:

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

    【讨论】:

    • 另外,将 stylus.middleware 代码移到 connect.public 上方显然也不错。
    • ...你在哪里添加的?
    • 我同意@ScottDavidTesler,这个答案并不完整。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 1970-01-01
      • 2014-11-26
      • 2012-12-13
      • 1970-01-01
      • 2012-11-12
      • 2016-01-12
      • 1970-01-01
      • 2012-02-27
      相关资源
      最近更新 更多