Guest User

Untitled

a guest
Dec 8th, 2016
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. 'use strict';
  2. import path from 'path';
  3. import { Server } from 'http';
  4. import Express from 'express';
  5. import React from 'react';
  6. import { renderToString } from 'react-dom/server';
  7. import { match, RouterContext } from 'react-router';
  8. var httpProxy = require('http-proxy');
  9. import * as ROUTES from "./routes";
  10. import { routes } from "./routes";
  11. import DataWrapper from './components/DataWrapper'
  12. import * as UrlHelper from "./config/UrlHelper";
  13. import _ from 'lodash';
  14. import url from 'url';
  15.  
  16. var apiProxy = httpProxy.createProxyServer();
  17.  
  18. // initialize the server and configure support for ejs templates
  19. const app = new Express();
  20. const server = new Server(app);
  21. app.set('view engine', 'ejs');
  22. app.set('views', path.join(__dirname, 'views'));
  23.  
  24. app.use(require('morgan')('short'));
  25.  
  26. // define the folder that will be used for static assets
  27. app.use(Express.static(path.join(__dirname, 'static')));
  28.  
  29. app.all('/api/*', (req, res) => {
  30. const path = _.drop(req.url.split('/'), 3);
  31. apiProxy.proxyRequest(req, res, {
  32. target: url.resolve('http://example/api/', path.join('/')),
  33. ignorePath: true,
  34. headers: {
  35. 'Authorization': 'XXX',
  36. },
  37. });
  38. });
  39.  
  40.  
  41. // Step 1: Create & configure a webpack compiler
  42. var webpack = require('webpack');
  43. var webpackConfig = require('../webpack.config');
  44. var compiler = webpack(webpackConfig);
  45.  
  46. console.log("public path", webpackConfig.output.publicPath);
  47.  
  48. // Step 2: Attach the dev middleware to the compiler & the server
  49. app.use(require("webpack-dev-middleware")(compiler, {
  50. noInfo: true, publicPath: webpackConfig.output.publicPath
  51. }));
  52.  
  53. // Step 3: Attach the hot middleware to the compiler & the server
  54. app.use(require("webpack-hot-middleware")(compiler, {
  55. log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
  56. }));
  57.  
  58.  
  59.  
  60. // universal routing and rendering
  61. app.get('*', (req, res) => {
  62. match(
  63. { routes, location: req.url },
  64. (err, redirectLocation, renderProps) => {
  65.  
  66. // in case of error display the error message
  67. if (err) {
  68. return res.status(500).send(err.message);
  69. }
  70. // in case of redirect propagate the redirect to the browser
  71. if (redirectLocation) {
  72. return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
  73. }
  74. // generate the React markup for the current route
  75. let markup = "";
  76. if (renderProps) {
  77. // if the current route matched we have renderProps
  78. let preloadedData = [];
  79. let metaData = {
  80. meta_desc: "",
  81. meta_title: "",
  82. meta_robots: ""
  83. }
  84.  
  85. switch (renderProps.routes[1].path) {
  86. case ROUTES.HOME:
  87. let schemeNameId = renderProps.params.fundName;
  88. // get data from api and load it in preloaded data
  89. preloadedData = {} // data from api
  90. let markup = renderToString(<DataWrapper data={preloadedData}><RouterContext {...renderProps} /></DataWrapper>);
  91. const meta_robots = metaData.meta_robots;
  92. const meta_title = metaData.meta_title;
  93. const meta_desc = metaData.meta_desc;
  94. return res.render('index', {
  95. meta_robots,
  96. meta_title,
  97. meta_desc,
  98. markup,
  99. preloadedData: JSON.stringify(preloadedData)
  100. });
  101.  
  102. }
  103. }
  104. }
  105. );
  106. });
Add Comment
Please, Sign In to add comment