So we’re coming up on the end of the FreeCodeCamp ExpressJS tutorial. This one is simple, almost too easy!
Here’s the directions and hints:
npm install stylus
Okay, let’s start by installing the stylus package.
└── glob@7.0.3 (path-is-absolute@1.0.0, inherits@2.0.1, inflight@1.0.5, once@1.3.3, minimatch@3.0.0)
We want to use this package to ‘style’ the HTML file we are serving. Stylus is middleware, which I talked about here. Let’s set up the outline of an express app:
var express = require('express'); var port = process.argv[2]; var filePath = process.argv[3]; var app = express(); app.listen(port);
Remember that they gave us the port and the filePath in the hints.
We are going to need to access the HTML file, and the stylus file. This is a static HTML file, and we’ll serve it using a command we’ve seen before:
app.use(express.static(filePath||path.join(__dirname, 'public'))); Note that since we're using path.join here, we'll need to add a require statement for it: <div> var path = require('path');
Next, we want to take the line they gave us in the hints to get the stylus file:
app.use(require('stylus').middleware(__dirname + '/public'));
And that's really it! Here's our final program:
var express = require('express'); var path = require('path'); var port = process.argv[2]; var filePath = process.argv[3]; var app = express(); app.use(express.static(filePath||path.join(__dirname, 'public'))); app.use(require('stylus').middleware(__dirname + '/public')); app.listen(port);
See you next time!