diff options
Diffstat (limited to 'front/odiparpack/server/index.js')
| -rw-r--r-- | front/odiparpack/server/index.js | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/front/odiparpack/server/index.js b/front/odiparpack/server/index.js new file mode 100644 index 0000000..9fb9ba6 --- /dev/null +++ b/front/odiparpack/server/index.js @@ -0,0 +1,78 @@ +/* eslint consistent-return:0 */ + +const express = require('express'); +const favicon = require('serve-favicon'); +const path = require('path'); +const logger = require('./logger'); +const rawicons = require('./rawicons'); +const rawdocs = require('./rawdocs'); +const argv = require('./argv'); +const port = require('./port'); +const setup = require('./middlewares/frontendMiddleware'); +const isDev = process.env.NODE_ENV !== 'production'; +const ngrok = (isDev && process.env.ENABLE_TUNNEL) || argv.tunnel + ? require('ngrok') + : false; +const { resolve } = require('path'); +const app = express(); + +// If you need a backend, e.g. an API, add your custom backend-specific middleware here +// app.use('/api', myApi); +// Load material icons +app.use('/api/icons', (req, res) => { + res.json({ + records: [ + { source: rawicons(req.query) } + ] + }); +}); + +// Load code preview +app.use('/api/docs', (req, res) => { + res.json({ + records: [ + { source: rawdocs(req.query) } + ] + }); +}); + +app.use('/', express.static('public', { etag: false })); +app.use(favicon(path.join('public', 'favicons', 'favicon.ico'))); + +// In production we need to pass these values in instead of relying on webpack +setup(app, { + outputPath: resolve(process.cwd(), 'build'), + publicPath: '/', +}); + +// get the intended host and port number, use localhost and port 3000 if not provided +const customHost = argv.host || process.env.HOST; +const host = customHost || null; // Let http.Server use its default IPv6/4 host +const prettyHost = customHost || 'localhost'; + +// use the gzipped bundle +app.get('*.js', (req, res, next) => { + req.url = req.url + '.gz'; // eslint-disable-line + res.set('Content-Encoding', 'gzip'); + next(); +}); + +// Start your app. +app.listen(port, host, async err => { + if (err) { + return logger.error(err.message); + } + + // Connect to ngrok in dev mode + if (ngrok) { + let url; + try { + url = await ngrok.connect(port); + } catch (e) { + return logger.error(e); + } + logger.appStarted(port, prettyHost, url); + } else { + logger.appStarted(port, prettyHost); + } +}); |
