Express.js"app.use()需要中间件功能" [英] Express.js "app.use() requires middleware function"

查看:82
本文介绍了Express.js"app.use()需要中间件功能"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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...

  1. 此错误是什么意思?

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆