【发布时间】:2014-02-25 18:00:25
【问题描述】:
我现在正试图弄清楚如何将 node.js 与翡翠一起使用,但我似乎无法将其拉入 css 样式表。我的 app.js 看起来像这样:
/* * Module dependencies */
var http = require("http"),
express = require('express') ,
stylus = require('stylus')
//create an app server
var app = module.exports = express.createServer();
app.configure(function(){
//set path to the views (template) directory
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(app.router);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(stylus.middleware({
debug: true,
src: __dirname + '/views',
dest: __dirname + '/public'
}));
app.use(express.static(__dirname + 'public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//handle GET requests on /
app.get('/', function(req, res){res.render('index.jade');
});
//listen on localhost:3000
app.listen(3000);
我的 layout.jade 文件如下所示:
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
而我的文件结构是:
App
-node_modules
-public
-css
style.css
-img
-js
-views
-routes
-stylesheets
style.styl
index.jade
layout.jade
app.js
package.json
我正在让网站正确加载,但样式表没有加载。
【问题讨论】:
-
给
express.static()的路径缺少/--__dirname + '/public'-- 所以它可能试图在不存在的Apppublic文件夹中查找文件。pathmodule 也可以帮助避免这种情况:express.static(path.join(__dirname, 'public'))。 -
我把它改成了:app.use(express.static(path.join(__dirname, 'public')));但是当我进行更改时,我收到“ReferenceError: path is not defined”。
-
对不起。您必须像
http、express等一样使用require('path'),因为它不是模块中的 global。 -
好的,添加:path = require('path') 并且它正在运行,但似乎仍然没有包含样式表。