Enabling Stylus in Express

Although Stylus is the recommended CSS preprocessor for Express, it does not come baked into Express by default. However, adding Stylus support in Express is very easy.

The first step is to install the Stylus npm package in the application directory:

$ npm install stylus

The next step is to include the Stylus middleware and the static middleware in the application. It is recommended to add them right after the router middleware:

app.use(app.router);
app.use(require('stylus').middleware('./public'));
app.use(express.static('./public'));

By passing a single parameter to the middleware() method, we specify where to find the Stylus files. When a request for a CSS file is made to the app, it will look for the corresponding ...

Get Express Web Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.