Skip to content

Instantly share code, notes, and snippets.

@maman

maman/server.js Secret

Last active September 4, 2019 13:06
Show Gist options
  • Save maman/8e91e6cb4ca9feaa4290 to your computer and use it in GitHub Desktop.
Save maman/8e91e6cb4ca9feaa4290 to your computer and use it in GitHub Desktop.
/*eslint-disable */
var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require(path.join(__dirname, 'webpack.config'));
var app = this.app = new express();
var compiler = webpack(config);
var devMiddleware = require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
})
/*eslint-enable */
app.use(this.middleware = devMiddleware);
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', function(req, res) {
/*eslint-disable */
var index = this.middleware.fileSystem.readFileSync(path.join(config.output.path, 'index.html'));
/*eslint-enable */
res.end(index);
}.bind(this));
app.listen(3001, 'localhost', function(err) {
if (err) {
console.log('err');
return;
}
console.log('Listening at http://localhost:3001');
});
@anselmo
Copy link

anselmo commented Nov 13, 2015

  • Gold

@jjoos
Copy link

jjoos commented Apr 18, 2016

Thank you!

@moodysalem
Copy link

moodysalem commented Nov 22, 2016

Better yet, put this in your webpack config and use with webpack-dev-server

devServer: {
    historyApiFallback: true,

    host: '0.0.0.0',
    port: 3000,

    setup(app) {
      app.get('/config.json', (req, res) => {
        res.json(CONFIG_VARIABLES);
      });
    }
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment