Express.js"app.use()需要中间件功能" [英] Express.js "app.use() requires middleware function"
问题描述
我正在学习Express.js 4和Node,但遇到一个我无法弄清的错误.
I'm learning Express.js 4 and Node, and I'm getting an error that I haven't been able to figure out.
我正在尝试使用node-sass包来编译我的sass代码,但是我无法启动它并运行它.
I'm trying to use the node-sass package to compile my sass code, but I haven't been able to get it up and running.
这是我主文件的精简版本:
Here is a stripped down version of my main file:
var express = require('express');
var sass = require('node-sass');
var app = express();
app.use(sass.middleware({
src: __dirname + "/assets",
dest: __dirname + "/static",
debug: true
}));
app.use(express.static(__dirname + '/static'));
app.get('/', function(req, res){
res.send("Hello World");
});
var server = app.listen(3000, function() {
console.log("Node is now listening.");
});
当我使用node server.js
运行此代码时,我又回想起一个错误:
When I run this with node server.js
, I get an error thrown back at me:
TypeError: app.use() requires middleware functions
据此,我假设sass.middleware
不是中间件功能...
From this, I'm assuming that sass.middleware
is not a middleware function...
-
此错误是什么意思?
What does this error mean?
为什么要扔它?
我该如何解决?
我能找到的最接近的答案来自此问题,但根据我从此处收集的信息来看,这些答案仅适用使用Express 3.
The closest answer I've been able to find was from this question, but from what I gleaned from here is that these answers will only work with Express 3.
如果有人能为我指出如何将Express 4和Sass一起设置的正确方向(如果确实可行),我将不胜感激.
I would much appreciate if someone could point me in the right direction for how to setup Express 4 and Sass together (if indeed this is possible).
推荐答案
node-sass
自述文件提到 node-sass-middleware
.因此,如果您npm install node-sass-middleware
然后执行:
The node-sass
readme mentions that the middleware has been moved to node-sass-middleware
. So if you npm install node-sass-middleware
and then do:
var sassMiddleware = require('node-sass-middleware');
app.use(sassMiddleware({
src: __dirname + "/assets",
dest: __dirname + "/static",
debug: true
}));
应该可以.
这篇关于Express.js"app.use()需要中间件功能"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!