Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 将Stylus文件输出到express3中的自定义目录大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有我的项目,并将一些代码从express2.5.7移植到express3.0.3.我认为它几乎是1:1的转移,但我遇到了一个无法将我的手写笔文件编译到我指定的目录中的问题.这是我的基本app.js设置:

/**
 * Module dependencies.
 */

var express = require('express'),routes = require('./routes'),user = require('./routes/user'),http = require('http'),path = require('path'),nib = require('nib'),bootstrap = require('bootstrap-stylus'),stylus = require('stylus');

var app = module.exports = express();

app.configure('dev',function(){
  var stylusMiddleware = stylus.middleware({
    src: __dirname + '/stylus/',// .styl files are located in `/stylus`
    dest: __dirname + '/public/css/',// .styl resources are compiled `/css/*.css`
    debug: true,compile: function(str,path) { // optional,but recommended
      console.log(path);
      return stylus(str)
        .set('filename',path)
        //.set('warn',truE)
        .set('compress',truE)
        .use(bootstrap())
    }
  });
  app.use(express.logger('dev'));
  app.use(stylusMiddlewarE);  
  app.use(express.errorHandler({ dumpExceptions: true,showStack: true })); 
  app.set('view options',{ pretty: true });
});

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

app.configure(function(){
  app.set('port',process.env.PORT || 3000);
  app.set('views',__dirname + '/views');
  app.set('view ENGIne','jade');
  app.use(express.favicon());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname,'public')));
});

app.get('/',routes.indeX);
app.get('/users',user.list);

http.createServer(app).listen(app.get('port'),function(){
  console.log("Express server listening on port " + app.get('port'));
});

我已经测试了app.configure的东西,它正在通过正确的方法(‘dev’和只有一个函数的configurE)

解决方法

我尝试设置自定义src和dest也不能.为了寻找解决方案,我查看了Stylus源代码.用两个词来描述styl和css文件应该是相似的.如果链接到html中的css文件看起来像

<link rel="stylesheet" href="css/app.css">

它的物理路径是

public/css/app.css

那么你的styl文件应该位于

stylus/css/app.styl

和Express配置应该是

app.configure('dev',function () {
  ...
  app.use(stylus.middleware({
    src: __dirname + '/stylus/',dest: __dirname + '/public/',path) { ... }
  }));
  ...
});

我在source中看到的.

Stylus解析所有请求并仅选择那些请求的css文件.然后它将css url的路径名与你的dest选项结合起来,用路径名中的styl替换css,并将结果与​​你的src选项结合起来:

// Middleware
return function stylus(req,res,next){
  if ('GET' != req.method && 'HEAD' != req.method) return next();
  var path = url.parse(req.url).pathname;
  if (/\.css$/.test(path)) {
    var cssPath = join(dest,path),stylusPath = join(src,path.replace('.css','.styl'));

    // ...

  } else {
    next();
  }
}

大佬总结

以上是大佬教程为你收集整理的node.js – 将Stylus文件输出到express3中的自定义目录全部内容,希望文章能够帮你解决node.js – 将Stylus文件输出到express3中的自定义目录所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。