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

发布于 2024-11-15 11:57:53 字数 935 浏览 2 评论 0原文

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

My app.coffee looks like this:

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}"

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦魇绽荼蘼 2024-11-22 11:57:53

找到答案,补充:

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

Found the answer, added:

compile = (str, path, fn) ->
  stylus(str).set('filename', path).set('compress', true)
旧故 2024-11-22 11:57:53

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

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

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文