I'm facing an issue with my .css file that was generated using Koala from a .scss file and added to my index.html. Unfortunately, the page is not serving my file because I forgot to add it as static on my Express server. I have tried following the various options listed in the Express server documentation http://expressjs.com/es/starter/static-files.html:
1)
app.use(express.static('public'));
However, I used:
app.use(express.static('styles/css'));
2)
app.use('/static', express.static('public'));
But I ended up using:
app.use('/static', express.static('styles/css'));
3)
app.use('/static', express.static(__dirname + 'public'));
Instead, I used:
app.use('/static', express.static(__dirname + '/styles/css'));
Unfortunately, none of these methods seem to be working for me despite trying different combinations. It's clear that I must be doing something wrong.
This is the structure of my folders:
project
src
styles
bower_components
css
app.css
sass
app.sass
app.js
index.html
package.json
server.js
webpack.config.js
Here is a snippet of my express server.js file:
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config');
const app = express();
const compiler = webpack(config);
const middleware = webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
stats: { colors: true },
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', (req, res) => {
const renderHtml = () => middleware.fileSystem
.readFileSync(path.join(compiler.outputPath, 'index.html'))
.toString();
res.send(renderHtml());
});
app.use('/static', express.static(path.join(__dirname, '/styles/css')))
app.listen(3000, '0.0.0.0', function (err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://0.0.0.0:3000');
});
I would appreciate any guidance on how to correctly use the express static line in this scenario. What am I missing or doing incorrectly? Thank you for your help in advance.