Skip to content

Instantly share code, notes, and snippets.

@kjs3
Created December 3, 2015 20:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kjs3/936a1a4a90dc36f407a2 to your computer and use it in GitHub Desktop.
Save kjs3/936a1a4a90dc36f407a2 to your computer and use it in GitHub Desktop.
Reloading express routes/middleware on file change WITHOUT restarting node process.
if ( process.env.NODE_ENV === 'production' ) {
// dev runs with babel-node so this isn't necessary
require('babel-core/register'); // this includes the polyfill
require('newrelic');
}
import 'source-map-support/register'
import Debug from 'debug'
import express from 'express'
import request from 'request'
import logger from 'morgan'
import cookieParser from 'cookie-parser'
import fs from 'fs'
import path from 'path'
import compression from 'compression'
import favicon from 'serve-favicon'
import swig from 'swig'
import chokidar from 'chokidar'
import webpack from 'webpack'
import webpackDevConfig from '../../build_config/webpack.dev.config'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
const webpackDevCompiler = webpack(webpackDevConfig);
const debug = Debug('server');
const server = express();
// setup webpack middleware and server-side hot-reloading for development
if ( process.env.NODE_ENV === 'development' ) {
server.use(webpackDevMiddleware(webpackDevCompiler, {
noInfo: true,
publicPath: webpackDevConfig.output.publicPath,
stats: {
hash: false,
version: false,
timings: true,
assets: false,
chunks: false,
modules: false,
}
}));
server.use(webpackHotMiddleware(webpackDevCompiler));
// load middleware
server.use(function(req, res, next) {
require('./middleware')(req, res, next);
});
// load routes
server.use(function(req, res, next) {
require('./routes')(req, res, next);
});
// Do "hot-reloading" of express stuff on the server
// Throw away cached modules and re-require next time
// Ensure there's no important state in there!
chokidar.watch(__dirname, {ignored: /index\.js$/})
.on('change', (path) => {
debug(`Clearing ${path} module cache from server`);
if (require.cache[path]) delete require.cache[path];
});
}
if ( process.env.NODE_ENV === 'production' ) {
server.use(logger('combined'));
} else {
server.use(logger('dev'));
}
// gzip responses
server.use(compression());
//
// TEMPLATE SETUP
//
// assign the swig engine to .html files
server.engine('html', swig.renderFile);
// set .html as the default extension
server.set('view engine', 'html');
server.set('views', path.join(__dirname, '../html'));
// Swig will cache templates for you, but you can disable
// that and use Express's caching instead, if you like:
server.set('view cache', true);
// To disable Swig's cache, do the following:
swig.setDefaults({ cache: false });
// NOTE: You should always cache templates in a production environment.
// Don't leave both of these to `false` in production!
server.use(favicon(path.join(__dirname, '../../public/favicon.ico')));
// return static files if found
const oneYearInMilliseconds = 31536000000;
server.use(express.static(path.join(__dirname, '../../build_public'), {maxAge: oneYearInMilliseconds}));
server.use(express.static(path.join(__dirname, '../../public'), {maxAge: oneYearInMilliseconds}));
server.use(cookieParser());
server.set('port', process.env.PORT || 3000);
// start server
server.listen(server.get('port'), function () {
const env = server.get('env');
const port = this.address().port;
debug('Express server running Rentals ' + server.get('env') + ' on port %s', port);
if ( process.env.NODE_ENV === 'development' ) debug('Please wait, Webpack is compiling…');
});
@filipemeneses
Copy link

Do you have the webpack.dev.config file?

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