我有一个运行较少中间件的node.js服务器。根据我的理解,它会动态编译并将css文件放在目标/相同(如果未指定)文件夹中。
我的问题是我在css文件的get请求中遇到404错误:
错误:GET http://webserver/public/less/blog-reset.css 404 (Not Found)
以下是我正在使用的内容:
web.js
//requiring dependencies
var express = require("express");
var logfmt = require("logfmt");
var lessMiddleware = require('less-middleware');
var hogan = require('hogan-express');
var path = require('path');
//all environments
var app = module.exports = express();
var port = Number(process.env.PORT || 5000);
app.use(logfmt.requestLogger());
app.use(lessMiddleware(path.join(__dirname,'public')));
app.use(express.static(path.join(__dirname,'public')));
app.set('layout',path.join(__dirname,'src','views','blog-layout'));
app.enable('view cache');
app.engine('.html',hogan);
//page routing called after env loads
require('./src/router');
//listening port
app.listen(port, function() {
console.log("Listening on " + port);
});
博客-的layout.html
<head>
<title>EpiBlog</title>
<link href='/public/less/blog-reset.css' rel='stylesheet' type='text/css'/>
</head>
<body>
{{{yield}}}
</body>
目录布局
ROOT
public
less
src
web.js
版本
我尝试了什么:
app.use(lessMiddleware)({ src: __dirname + '/public' }));
(显然是旧方法)app.use(lessMiddleware(path.join(__dirname,'public','less')));
app.use(express.static(path.join(__dirname,'public')));
从web.js移至router.js 这样:
app.use(lessMiddleware(path.join(__dirname, 'source', 'less'), {
dest: path.join(__dirname, 'public')
}));
答案 0 :(得分:4)
<link href='/public/less/blog-reset.css' rel='stylesheet' type='text/css'/>
应该是:
<link href='/less/blog-reset.css' rel='stylesheet' type='text/css'/>
我读到了:
link(rel='stylesheet', type='text/css', href='css/styles.css')
与目录结构配对:
myapp
+-public
+-css
+-styles.less
让我相信这个电话:
app.use(express.static(path.join(__dirname,'public')));
使请求假定/public/
是父级,因此我是多余的调用/public/less/blog-reset.css