Reputation:
I'm having a hard time trying to figure out why my server returns errors when I start it. I'm not using webpack-dev-server because it doesn't compile JS files so I decided to make my own server. When I opened my browser, it displayed an error message that says:
server.js
:
var webpack = require('webpack'),
path = require('path'),
express = require('express'),
devMiddleware = require('webpack-dev-middleware'),
hotMiddleware = require('webpack-hot-middleware'),
config = require('./webpack.config'),
app = express(),
host = '127.0.0.1',
port = 8000;
var middleware = devMiddleware(webpack({
entry: path.join(__dirname, 'app/src/main.js'),
output: {
path: '/',
}
}), {
noInfo: true,
publicPath: config.output.publicPath,
index: path.join(__dirname, 'app/index.html'),
stats: {
colors: true
}
});
app.use(middleware);
app.use(hotMiddleware(webpack(config)));
app.get('/', function(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'app/index.html')));
res.end();
});
app.listen(port, host, function(err) {
if (err) {
console.error(err);
}
console.log('Listening on ' + host + ':' + port);
});
project files and directories:
I don't understand why this is happening. Maybe I'm missing a lot of important plugins or dependencies to make it successful like react-hot-loader? Or it doesn't matter?
Upvotes: 1
Views: 74
Reputation: 203231
From what I can see, you shouldn't be using middleware.fileSystem.readFileSync()
to handle requests for index.html
(which isn't part of your bundle).
Just use fs.readFileSync()
or, preferably, res.sendFile()
:
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'app/index.html'));
});
Upvotes: 1