diff options
| author | gabrhr <[email protected]> | 2022-04-20 10:19:29 -0500 |
|---|---|---|
| committer | gabrhr <[email protected]> | 2022-04-20 10:19:29 -0500 |
| commit | e13e630cd6e4fc0b1ff92098a28a770794c7bb9a (patch) | |
| tree | e68ad2f947d1b3ec454529b35f37ca2f223e5431 /front/odiparpack/server/middlewares | |
| parent | 457816ac1129fcc6019d2fc795b6693ee6776d59 (diff) | |
| download | DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.gz DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.bz2 DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.zip | |
AƱadir plantilla
Base para front
Diffstat (limited to 'front/odiparpack/server/middlewares')
3 files changed, 75 insertions, 0 deletions
diff --git a/front/odiparpack/server/middlewares/addDevMiddlewares.js b/front/odiparpack/server/middlewares/addDevMiddlewares.js new file mode 100644 index 0000000..495b4a2 --- /dev/null +++ b/front/odiparpack/server/middlewares/addDevMiddlewares.js @@ -0,0 +1,38 @@ +const path = require('path'); +const webpack = require('webpack'); +const webpackDevMiddleware = require('webpack-dev-middleware'); +const webpackHotMiddleware = require('webpack-hot-middleware'); + +function createWebpackMiddleware(compiler, publicPath) { + return webpackDevMiddleware(compiler, { + logLevel: 'warn', + publicPath, + silent: true, + stats: 'errors-only', + }); +} + +module.exports = function addDevMiddlewares(app, webpackConfig) { + const compiler = webpack(webpackConfig); + const middleware = createWebpackMiddleware( + compiler, + webpackConfig.output.publicPath, + ); + + app.use(middleware); + app.use(webpackHotMiddleware(compiler)); + + // Since webpackDevMiddleware uses memory-fs internally to store build + // artifacts, we use it instead + const fs = middleware.fileSystem; + + app.get('*', (req, res) => { + fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => { + if (err) { + res.sendStatus(404); + } else { + res.send(file.toString()); + } + }); + }); +}; diff --git a/front/odiparpack/server/middlewares/addProdMiddlewares.js b/front/odiparpack/server/middlewares/addProdMiddlewares.js new file mode 100644 index 0000000..0a5bca5 --- /dev/null +++ b/front/odiparpack/server/middlewares/addProdMiddlewares.js @@ -0,0 +1,18 @@ +const path = require('path'); +const express = require('express'); +const compression = require('compression'); + +module.exports = function addProdMiddlewares(app, options) { + const publicPath = options.publicPath || '/'; + const outputPath = options.outputPath || path.resolve(process.cwd(), 'build'); + + // compression middleware compresses your server responses which makes them + // smaller (applies also to assets). You can read more about that technique + // and other good practices on official Express.js docs http://mxs.is/googmy + app.use(compression()); + app.use(publicPath, express.static(outputPath)); + + app.get('*', (req, res) => + res.sendFile(path.resolve(outputPath, 'index.html')), + ); +}; diff --git a/front/odiparpack/server/middlewares/frontendMiddleware.js b/front/odiparpack/server/middlewares/frontendMiddleware.js new file mode 100644 index 0000000..4d01858 --- /dev/null +++ b/front/odiparpack/server/middlewares/frontendMiddleware.js @@ -0,0 +1,19 @@ +/* eslint-disable global-require */ + +/** + * Front-end middleware + */ +module.exports = (app, options) => { + const isProd = process.env.NODE_ENV === 'production'; + + if (isProd) { + const addProdMiddlewares = require('./addProdMiddlewares'); + addProdMiddlewares(app, options); + } else { + const webpackConfig = require('../../internals/webpack/webpack.dev.babel'); + const addDevMiddlewares = require('./addDevMiddlewares'); + addDevMiddlewares(app, webpackConfig); + } + + return app; +}; |
