therealrootuser
therealrootuser

Reputation: 10914

Express.js "app.use() requires middleware function"

I'm learning Express.js 4 and Node, and I'm getting an error that I haven't been able to figure out.

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.");
});

When I run this with node server.js, I get an error thrown back at me:

TypeError: app.use() requires middleware functions

From this, I'm assuming that sass.middleware is not a middleware function...

  1. What does this error mean?

  2. Why is it being thrown?

  3. How do I fix it?


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.

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).

Upvotes: 2

Views: 9471

Answers (1)

mscdex
mscdex

Reputation: 106696

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
}));

it should work.

Upvotes: 8

Related Questions