scratch-www/dev-server/index.js

43 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-01-17 15:40:27 -05:00
const express = require('express');
const proxy = require('express-http-proxy');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpack = require('webpack');
const compiler = webpack(require('../webpack.config.js'));
const handler = require('./handler');
const log = require('./log');
const routes = require('../src/routes.json').concat(require('../src/routes-dev.json'))
2018-03-09 08:49:56 -05:00
.filter(route => !process.env.VIEW || process.env.VIEW === route.view);
// Create server
2024-01-17 15:40:27 -05:00
const app = express();
app.disable('x-powered-by');
// Server setup
app.use(log());
// Bind routes
routes.forEach(route => {
app.get(route.pattern, handler(route));
});
2024-01-17 15:40:27 -05:00
const middlewareOptions = {};
app.use(webpackDevMiddleware(compiler, middlewareOptions));
2024-01-17 15:40:27 -05:00
const proxyHost = process.env.FALLBACK || '';
if (proxyHost !== '') {
// Fall back to scratchr2 in development
// This proxy middleware must come last
app.use('/', proxy(proxyHost));
}
// Start listening
2024-01-17 15:40:27 -05:00
const port = process.env.PORT || 8333;
app.listen(port, () => {
process.stdout.write(`Server listening on port ${port}\n`);
if (proxyHost) {
2024-01-17 15:40:27 -05:00
process.stdout.write(`Proxy host: ${proxyHost}\n`);
}
});