Skip to content

Instantly share code, notes, and snippets.

@kjs3 kjs3/server.js

Created Dec 3, 2015
What would you like to do?
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
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/'
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,
// 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!, {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' ) {
} else {
// gzip responses
// 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.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…');

This comment has been minimized.

Copy link

filipemeneses commented Nov 15, 2017

Do you have the file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.