|
| 1 | +require('ignore-styles'); |
| 2 | +const babelRegister = require('babel-register'); |
| 3 | +const proxy = require('http-proxy-middleware'); |
| 4 | + |
| 5 | +babelRegister({ |
| 6 | + ignore: /\/(build|node_modules)\//, |
| 7 | + presets: ['react-app'], |
| 8 | +}); |
| 9 | + |
| 10 | +const express = require('express'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +const app = express(); |
| 14 | + |
| 15 | +// Application |
| 16 | +if (process.env.NODE_ENV === 'development') { |
| 17 | + app.get('/', function (req, res) { |
| 18 | + // In development mode we clear the module cache between each request to |
| 19 | + // get automatic hot reloading. |
| 20 | + for (var key in require.cache) { |
| 21 | + delete require.cache[key]; |
| 22 | + } |
| 23 | + const render = require('./render').default; |
| 24 | + render(req.url, res); |
| 25 | + }); |
| 26 | +} else { |
| 27 | + const render = require('./render').default; |
| 28 | + app.get('/', function (req, res) { |
| 29 | + render(req.url, res); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +// Static resources |
| 34 | +app.use(express.static(path.resolve(__dirname, '..', 'build'))); |
| 35 | + |
| 36 | +// Proxy everything else to create-react-app's webpack development server |
| 37 | +if (process.env.NODE_ENV === 'development') { |
| 38 | + app.use( |
| 39 | + '/', |
| 40 | + proxy({ |
| 41 | + ws: true, |
| 42 | + target: 'http://localhost:3001', |
| 43 | + }) |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +app.listen(3000, () => { |
| 48 | + console.log('Listening on port 3000...'); |
| 49 | +}); |
| 50 | + |
| 51 | +app.on('error', function (error) { |
| 52 | + if (error.syscall !== 'listen') { |
| 53 | + throw error; |
| 54 | + } |
| 55 | + |
| 56 | + var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; |
| 57 | + |
| 58 | + switch (error.code) { |
| 59 | + case 'EACCES': |
| 60 | + console.error(bind + ' requires elevated privileges'); |
| 61 | + process.exit(1); |
| 62 | + break; |
| 63 | + case 'EADDRINUSE': |
| 64 | + console.error(bind + ' is already in use'); |
| 65 | + process.exit(1); |
| 66 | + break; |
| 67 | + default: |
| 68 | + throw error; |
| 69 | + } |
| 70 | +}); |
0 commit comments