moonion_nashivan

Untitled

Jul 10th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import path from "path";
  2.  
  3. import {renderToString} from "react-dom/server";
  4. import React from 'react'
  5. import {StaticRouter} from "react-router-dom";
  6. import {Provider} from 'react-redux';
  7. import Express  from 'express';
  8. import {SheetsRegistry} from 'react-jss/lib/jss';
  9. import JssProvider from 'react-jss/lib/JssProvider';
  10. import {create} from 'jss';
  11. import preset from 'jss-preset-default';
  12. import createGenerateClassName from 'material-ui/styles/createGenerateClassName';
  13.  
  14. import {createExpressMiddleware} from "create-react-server";
  15.  
  16. import Parse from "parse";
  17.  
  18. import app from "./app";
  19. import configureStore from "./store";
  20. import config from "../webpack.config";
  21.  
  22. import template from "./template";
  23. import theme from "./theme";
  24. import sitemap from './sitemap';
  25. import robots from './robots';
  26.  
  27. import {getRedirectSpaceUrl, getRedirectUrlByType, getRedirectUrlByAmenities} from "./lib/url";
  28. import {TYPE_LIST} from "./space/constants/index";
  29.  
  30. const PARSE_APP_ID = 'pQpwnZzfnfDlTcVXkSPuZpMsSKccGIN0TLlq0hcq';
  31. const PARSE_SERVER_URL = 'http://parse-server/parse';
  32.  
  33. Parse.serverURL = PARSE_SERVER_URL;
  34. Parse.initialize(PARSE_APP_ID);
  35. let cashes = {};
  36.  
  37.  
  38. const options = {
  39.     render: (req, res, initialProps, context) => {
  40.         if(context.url)
  41.             return res.redirect(301,context.url);
  42.  
  43.         let path = req.url.substring(1).split('/');
  44.  
  45.  
  46.         if(path.length > 5 && path[path.length-1].indexOf('space-') > -1)
  47.             return res.redirect(301,getRedirectSpaceUrl(path));
  48.  
  49.         let index = path.findIndex(i => TYPE_LIST.indexOf(i) > 0);
  50.  
  51.         if(index === 2 || index === 3)
  52.             return res.redirect(301,getRedirectUrlByType(path, index));
  53.  
  54.         let amenitiesIndex = path.findIndex(a => a.indexOf('amenities-') > -1);
  55.  
  56.         if (amenitiesIndex > -1){
  57.             path.splice(amenitiesIndex, 1);
  58.             return res.redirect(301,getRedirectUrlByAmenities(path));
  59.         }
  60.        
  61.         if(path[0] === "locations")
  62.             return res.redirect(301,req.url.replace('locations', 'united-states'));
  63.  
  64.         if(path[0] === "location")
  65.             return res.redirect(301,req.url.replace('location', 'united-states'));
  66.  
  67.         if (path[0] === 'dropzones')
  68.             return res.redirect(301, req.url.replace('dropzones', 'host-a-pop-up-workspace'));
  69.  
  70.         if (path[0] === 'sign' && (path[1] === 'in' || path[1] === 'up'))
  71.             return path[1] === 'in' ? res.redirect(301, req.url.replace('sign/in', 'sign')) : res.redirect(301, req.url.replace('sign/up', 'sign'));
  72.  
  73.         const sheetsRegistry = new SheetsRegistry();
  74.         const jss = create(preset());
  75.         jss.options.createGenerateClassName = createGenerateClassName;
  76.         const state = context.store ? context.store.getState() : undefined;
  77.         const store = configureStore(state);
  78.         const Theme = theme(new Map());
  79.  
  80.         if (cashes[req.headers.host + req.url] && state && state.common.brand && state.common.brand.updatedAt === (cashes[req.headers.host + req.url].state.common.brand && cashes[req.headers.host + req.url].state.common.brand.updatedAt))
  81.             return cashes[req.headers.host + req.url];
  82.         let html = renderToString(React.createElement(
  83.             JssProvider,
  84.             {registry: sheetsRegistry, jss: jss},
  85.             React.createElement(
  86.                 Provider,
  87.                 {store: store},
  88.                 React.createElement(
  89.                     StaticRouter,
  90.                     {location: req.url, context: context},
  91.                     React.createElement(
  92.                         Theme,
  93.                         {},
  94.                         options.app({
  95.                             props: initialProps,
  96.                             req: req,
  97.                             res: res,
  98.                             state: state
  99.                         })
  100.                     )
  101.                 )
  102.             )
  103.         ));
  104.         let css = sheetsRegistry.toString();
  105.         if (state) cashes[req.headers.host + req.url] = {html, css, state};
  106.         return cashes[req.headers.host + req.url]
  107.  
  108.     },
  109.     app,
  110.     template,
  111.     templatePath: path.join(config.output.path, 'index.html'),
  112.     isNotEmbed: true,
  113.     outputPath: config.output.path,
  114.     port: process.env.PORT || 80,
  115.     debug: true
  116. };
  117.  
  118.  
  119.  
  120.  
  121. let port = options.port || 80;
  122. const appl = Express();
  123.  
  124. appl.get('/sitemap.xml', sitemap());
  125. appl.get('/robots.txt', robots());
  126. appl.use(createExpressMiddleware(options));
  127. appl.use(Express.static(config.output.path));
  128.  
  129.  
  130. appl.listen(port, (err)=>{
  131.     if (err) throw err;
  132.     console.log('Listening %s', port);
  133. });
Add Comment
Please, Sign In to add comment