简体   繁体   English

是否存在可以使用Express 4.0在节点Web服务器上编译LESS CSS的中间件?

[英]Is there middleware that can compile LESS CSS on a node webserver using Express 4.0?

Since the connect framework is not used anymore the less-middleware solution does not work anymore or otherwise I am doing something wrong. 由于不再使用连接框架,因此不再使用中间件解决方案,否则我做错了。

I am using express 4.0 and tried using less-middleware 1.0.4 however it does not work and it does not say anywhere that it is supported in express 4.0 since the connect framework is not supported anymore. 我正在使用Express 4.0,并尝试使用较少中间件1.0.4,但是它不起作用,也没有说Express 4.0支持它的任何地方,因为不再支持连接框架。

So is there middleware that can compile LESS CSS on a node webserver using express 4.0 that has sufficient documentation on how to do it or could you show me. 因此,是否存在可以使用express 4.0在节点Web服务器上编译LESS CSS的中间件,该中间件具有有关如何执行此操作的足够文档,或者您可以向我展示。 Thanks! 谢谢!

Code I was having issues with can be seen below. 我下面遇到的代码可以在下面看到。 If you can see something wrong, please let me know. 如果您发现有问题,请告诉我。

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 is HTML是

<link rel="stylesheet" type="text/css" href="/css/main.css" media="screen"/>

I have 'less-middleware' working fine in express 4.0. 我的“较少中间件”在Express 4.0中工作正常。 Code i'm using is: 我使用的代码是:

// Convert less to css on the fly
app.use(require('less-middleware')('public'));
// Public folder
app.use(express.static('public'));

After that any less file in public folder (or its subfolders) should be served as css. 之后,公用文件夹(或其子文件夹)中的任何较少文件都应用作CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM