Skip to content

Instantly share code, notes, and snippets.

@kjs3 kjs3/server.js
Created Dec 3, 2015

Embed
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
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

This comment has been minimized.

Copy link

commented Nov 15, 2017

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
You can’t perform that action at this time.