开发者

How to get Stylus to work with Express and Connect in CoffeeScript

My app.coffee looks like this:

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

app = express.createServer()

# CONFIGURATION

app.configure(() 开发者_如何学JAVA->
  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}"

Update: I don't get it to write the CSS-files at all.


Found the answer, added:

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


You can of course provide your own compile function, but it unnecessarily overrides the default one. Instead, add debug option to your middleware call and inspect where things are going wrong:

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

For me, the problem was setting the wrong src/dest path. Are you sure your .styl files are indeed located in your views folder?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜