dev.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const assets = require('../../common/assets');
  2. const routes = require('../routes');
  3. const pages = require('../routes/pages');
  4. const tests = require('../../test/frontend/routes');
  5. const express = require('express');
  6. const expressWs = require('@dannycoates/express-ws');
  7. const morgan = require('morgan');
  8. const config = require('../config');
  9. const ID_REGEX = '([0-9a-fA-F]{10, 16})';
  10. module.exports = function(app, devServer) {
  11. const wsapp = express();
  12. expressWs(wsapp, null, { perMessageDeflate: false });
  13. routes(wsapp);
  14. wsapp.ws('/api/ws', require('../routes/ws'));
  15. wsapp.listen(8081, config.listen_address);
  16. assets.setMiddleware(devServer.middleware);
  17. app.use(morgan('dev', { stream: process.stderr }));
  18. function android(req, res) {
  19. const index = devServer.middleware.fileSystem
  20. .readFileSync(devServer.middleware.getFilenameFromUrl('/android.html'))
  21. .toString()
  22. .replace(
  23. '<base href="file:///android_asset/" />',
  24. '<base href="http://localhost:8080/" />'
  25. );
  26. res.set('Content-Type', 'text/html');
  27. res.send(index);
  28. }
  29. if (process.env.ANDROID) {
  30. // map all html routes to the android index.html
  31. app.get('/', android);
  32. app.get(`/share/:id${ID_REGEX}`, android);
  33. app.get('/completed', android);
  34. app.get('/preferences', android);
  35. app.get('/options', android);
  36. app.get('/oauth', android);
  37. }
  38. routes(app);
  39. tests(app);
  40. // webpack-dev-server routes haven't been added yet
  41. // so wait for next tick to add 404 handler
  42. process.nextTick(() => app.use(pages.notfound));
  43. };