Node.js 手写笔仅在加载索引页时重新编译.styl文件
我最初在尝试设置手写笔时非常沮丧,因为我会调整我的Node.js 手写笔仅在加载索引页时重新编译.styl文件,node.js,express,stylus,Node.js,Express,Stylus,我最初在尝试设置手写笔时非常沮丧,因为我会调整我的src和dest设置,重新启动节点,刷新,手写笔没有编译。这是在一个类似http://localhost:3000/tasks。但是,src和dest路径是正确的,当我重新启动节点并尝试加载索引页时,http://localhost:3000,然后手写笔将正确编译 现在我已经知道它是正确编译的,但只是从主URL,我想知道我是否设置了错误,因为对.styl文件的任何更改都不会更新,直到我从主页刷新,而不是从任何GET参数页刷新 var expres
src
和dest
设置,重新启动节点,刷新,手写笔没有编译。这是在一个类似http://localhost:3000/tasks
。但是,src
和dest
路径是正确的,当我重新启动节点并尝试加载索引页时,http://localhost:3000
,然后手写笔将正确编译
现在我已经知道它是正确编译的,但只是从主URL,我想知道我是否设置了错误,因为对.styl文件的任何更改都不会更新,直到我从主页刷新,而不是从任何GET参数页刷新
var express = require('express');
var app = express();
var stylus = require('stylus');
app.configure(function () {
this.set("views", __dirname + "/views/jade");
this.set("view engine", "jade");
this.use(express.bodyParser());
this.use(express.methodOverride());
this.use(this.router);
this.use(stylus.middleware({
src: __dirname + '/views/styl', //styl files to be compiled
dest: __dirname + '/public/css', //destination for compiled css
compress: true
}));
this.use(express.static(__dirname + '/public'));
});
是我所描述的正常过程,还是无论您的URL是什么,如果它注意到
.styl
文件中的更改,触控笔都应该重新编译?我使用默认的触控笔路径,这意味着在我的“视图”和“公共”目录中使用名为“样式表”的目录,以及以下代码:
//Stylus
this.use(stylus.middleware({
src: __dirname + '/views', //styl files to be compiled
dest: __dirname + '/public', //destination for compiled css
compress: true
}));
然后,Stylus在/views/stylesheets中查找.styl文件,并将其编译为/public/stylesheets。出于某种原因,试图更改目录的名称和对路径的迷恋让我陷入了麻烦。从一些论坛的阅读判断,目前这似乎不被认为是一个bug,但它确实存在。我没有50岁的声誉可以评论,但你可以根据自己的意愿更改路径。首先需要
路径
模块:
path = require('path')
然后根据你的口味决定:
path.resolve('./') //this will be resolved to where your node app resides
path.resolve('../') //this will go one directory up from the path of your node app file
path.resolve('../../') //this will go two directories up from the path of your node app file
使用console.log()。然后,对于手写笔中的选项,只需将其更改为解析路径,而不是使用\u dirname
作为基础。因此,假设您有一个目录结构:
$userPath/myApp/frontend/src/app.js
$userPath/myApp/frontend/src/public
$userPath/myApp/frontend/static/public
然后你可以:
var staticPath = path.resolve('../') + '/static'; //$userPath/myApp/frontend/static
app.use(
stylus.middleware({
src: __dirname + '/public/',
dest: staticPath + '/public/',
debug: true,
compile : function(str, path) {
return stylus(str)
.set('filename', path)
.set('warn', true)
.set('compress', true);
}
})
);
另外,如果有人能让我知道,如果我的问题很难理解,我会重新声明。这是令人难以置信的沮丧…突然我的手写笔文件根本没有编译,我没有更改任何代码。FWIW,我根本不认识这一点。