|
| 1 | +import http from 'http'; |
1 | 2 | import express from 'express'; |
| 3 | +import colors from 'colors'; |
| 4 | + |
| 5 | +// Development Libraries |
| 6 | +// import webpack from 'webpack'; |
| 7 | +// import devWebpackConfig from '/webpack/webpack.config.dev'; |
| 8 | +import chokidar from 'chokidar'; |
2 | 9 |
|
3 | 10 | import { |
4 | 11 | renderPage, |
5 | 12 | renderDevPage |
6 | 13 | } from './ssr'; |
7 | 14 |
|
8 | | - |
9 | 15 | const PROD = process.env.NODE_ENV === 'production'; |
10 | 16 |
|
11 | 17 | const app = express(); |
12 | 18 |
|
13 | | - |
| 19 | +// Production settings |
14 | 20 | if (PROD) { |
15 | 21 | app.get('*', renderPage); |
| 22 | + |
| 23 | +// Development settings |
16 | 24 | } else if (!PROD) { |
| 25 | + // const compiler = webpack(devWebpackConfig); |
| 26 | + // |
| 27 | + // app.use(require('webpack-dev-middleware')(compiler, { |
| 28 | + // noInfo: true, |
| 29 | + // publicPath: devWebpackConfig.output.publicPath |
| 30 | + // })); |
| 31 | + // |
| 32 | + // app.use(require('webpack-hot-middleware')(compiler, { |
| 33 | + // log: console.log |
| 34 | + // })); |
| 35 | + |
17 | 36 | app.get('*', renderDevPage); |
| 37 | + |
| 38 | + // Do "hot-reloading" of express stuff on the server |
| 39 | + // Throw away cached modules and re-require next time |
| 40 | + // Ensure there's no important state in there! |
| 41 | + const watcher = chokidar.watch('.', {ignored: /[\/\\]node_modules[\/\\]/}); |
| 42 | + |
| 43 | + watcher.on('ready', function() { |
| 44 | + watcher.on('all', function() { |
| 45 | + Object.keys(require.cache).forEach(function(id) { |
| 46 | + if (/[\/\\]server[\/\\]/.test(id)) { |
| 47 | + console.log(`${'Clearing'.yellow} ${'/server/'.red+id.split(/[\/\\]server[\/\\]/)[1].red} ${'module cache from server'.yellow}`); |
| 48 | + delete require.cache[id]; |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + |
| 55 | + // Do "hot-reloading" of react stuff on the server |
| 56 | + // Throw away the cached client modules and let them be re-required next time |
| 57 | + // compiler.plugin('done', function() { |
| 58 | + // console.log("Clearing /client/ module cache from server"); |
| 59 | + // Object.keys(require.cache).forEach(function(id) { |
| 60 | + // if (/[\/\\]client[\/\\]/.test(id)) delete require.cache[id]; |
| 61 | + // }); |
| 62 | + // }); |
18 | 63 | } |
19 | 64 |
|
| 65 | +const server = http.createServer(app); |
20 | 66 |
|
21 | | -app.listen(3000, function () { |
22 | | - console.log('>>>> RUNNING ON localhost:3000'); |
23 | | -}); |
| 67 | +server.listen(3000, function() { |
| 68 | + const address = server.address(); |
| 69 | + console.log(`${'>>>'.cyan} ${'Listening on:'.rainbow} ${'localhost::'.trap.magenta}${`${address.port}`.green}`); |
| 70 | + }); |
0 commit comments