0
由於連接框架不再使用,較少的中間件解決方案不再工作,否則我做錯了什麼。是否有中間件可以使用Express 4.0在節點Web服務器上編譯LESS CSS?
我正在使用Express 4.0並嘗試使用less-middleware 1.0.4,但它不起作用,並且它不會在express 4.0支持它,因爲連接框架不再受支持。
那麼是否有中間件可以使用Express 4.0編譯節點Web服務器上的LESS CSS,該文件有足夠的文檔說明如何操作或可以向我展示。謝謝!
代碼我遇到問題可以在下面看到。如果您發現有問題,請告訴我。
var _ = require('lodash');
var express = require('express');
var http = require('http');
var path = require('path');
var app= express();
var fs = require('fs');
var mime = require('mime');
var lessMiddleware = require('less-middleware');
var cache = {};
//get the models
//require('./app_server/models/db');
//set the port to be used
app.set('port', process.env.PORT || 3000);
//set the view folders
app.set('views', path.join(__dirname, 'public/'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.set('view engine', 'jade');
//this should be removed in express 4.0
// app.use(app.router);
//sets the directory that the use can call data from (e.g. link css and js files)
console.log('__dirname', __dirname);
app.use(lessMiddleware(path.join(__dirname, "/app_server/less"), {
dest: __dirname + "/public/css",
// if you're using a different src/dest directory, you MUST
// include the prefix, which matches the dest public directory
prefix: "/css",
// force true recompiles on every request... not the best
// for production, but fine in debug while working through changes
force: true,
debug: true
}));
app.use(express.static(path.join(__dirname, 'public')));
//gets routes file that links routes to controllers
require('./routes')(app);
//server helper functions
function send404(response) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('Error 404: resource not found.');
response.end();
}
function sendFile(response, filePath, fileContents) {
response.writeHead(200, {"content-type": mime.lookup(filePath)});
response.end(fileContents);
}
function serveStatic(response, cache, absPath) {
if(cache[absPath]){
sendFile(response, absPath, cache[absPath]);
} else {
fs.exists(absPath, function (exists) {
if(exists){
fs.readFile(absPath, function (err, data) {
if(err){
send404(response);
} else {
cache[absPath] = data;
sendFile(response, absPath, data);
}
});
} else {
send404(response);
}
})
}
}
var server = http.createServer(function (request, response) {
var filePath = false;
if(!request.url.match(/^\/?(lib|stylesheets|partials|css)/gi)){
filePath = 'public/index.html';
} else {
filePath = 'public' + request.url;
}
var absPath = './' + filePath;
serveStatic(response, cache, absPath);
});
//make the server work (listen)
server.listen(process.env.PORT || 3000, function() {
// console.log('Server Listening on port 3000');
console.log('Express server listening on port ' + app.get('port') + ' and running in ' + app.get('env') + ' mode');
});
HTML是
<link rel="stylesheet" type="text/css" href="/css/main.css" media="screen"/>
在黑暗中刺傷,你有沒有嘗試過吞嚥,吞沒? (不完全確定它的使用服務器端) – Kiee
如果它沒有被服務器編譯,我不會覺得它有用。在澳大利亞,我們使用考拉。 –
您在中間件中設置* debug * true。它應該在運行應用程序時立即公開控制檯日誌。你在那看到什麼。還有一點,您可以像@Kiee建議的那樣使用[gulp-less](https://www.npmjs.com/package/gulp-less/)。這些是在您的Web應用程序構建時應該使用的節點模塊。 –