Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import path from "path";
- import {renderToString} from "react-dom/server";
- import React from 'react'
- import {StaticRouter} from "react-router-dom";
- import {Provider} from 'react-redux';
- import Express from 'express';
- import {SheetsRegistry} from 'react-jss/lib/jss';
- import JssProvider from 'react-jss/lib/JssProvider';
- import {create} from 'jss';
- import preset from 'jss-preset-default';
- import createGenerateClassName from 'material-ui/styles/createGenerateClassName';
- import {createExpressMiddleware} from "create-react-server";
- import Parse from "parse";
- import app from "./app";
- import configureStore from "./store";
- import config from "../webpack.config";
- import template from "./template";
- import theme from "./theme";
- import sitemap from './sitemap';
- import robots from './robots';
- import {getRedirectSpaceUrl, getRedirectUrlByType, getRedirectUrlByAmenities} from "./lib/url";
- import {TYPE_LIST} from "./space/constants/index";
- const PARSE_APP_ID = 'pQpwnZzfnfDlTcVXkSPuZpMsSKccGIN0TLlq0hcq';
- const PARSE_SERVER_URL = 'http://parse-server/parse';
- Parse.serverURL = PARSE_SERVER_URL;
- Parse.initialize(PARSE_APP_ID);
- let cashes = {};
- const options = {
- render: (req, res, initialProps, context) => {
- if(context.url)
- return res.redirect(301,context.url);
- let path = req.url.substring(1).split('/');
- if(path.length > 5 && path[path.length-1].indexOf('space-') > -1)
- return res.redirect(301,getRedirectSpaceUrl(path));
- let index = path.findIndex(i => TYPE_LIST.indexOf(i) > 0);
- if(index === 2 || index === 3)
- return res.redirect(301,getRedirectUrlByType(path, index));
- let amenitiesIndex = path.findIndex(a => a.indexOf('amenities-') > -1);
- if (amenitiesIndex > -1){
- path.splice(amenitiesIndex, 1);
- return res.redirect(301,getRedirectUrlByAmenities(path));
- }
- if(path[0] === "locations")
- return res.redirect(301,req.url.replace('locations', 'united-states'));
- if(path[0] === "location")
- return res.redirect(301,req.url.replace('location', 'united-states'));
- if (path[0] === 'dropzones')
- return res.redirect(301, req.url.replace('dropzones', 'host-a-pop-up-workspace'));
- if (path[0] === 'sign' && (path[1] === 'in' || path[1] === 'up'))
- return path[1] === 'in' ? res.redirect(301, req.url.replace('sign/in', 'sign')) : res.redirect(301, req.url.replace('sign/up', 'sign'));
- const sheetsRegistry = new SheetsRegistry();
- const jss = create(preset());
- jss.options.createGenerateClassName = createGenerateClassName;
- const state = context.store ? context.store.getState() : undefined;
- const store = configureStore(state);
- const Theme = theme(new Map());
- 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))
- return cashes[req.headers.host + req.url];
- let html = renderToString(React.createElement(
- JssProvider,
- {registry: sheetsRegistry, jss: jss},
- React.createElement(
- Provider,
- {store: store},
- React.createElement(
- StaticRouter,
- {location: req.url, context: context},
- React.createElement(
- Theme,
- {},
- options.app({
- props: initialProps,
- req: req,
- res: res,
- state: state
- })
- )
- )
- )
- ));
- let css = sheetsRegistry.toString();
- if (state) cashes[req.headers.host + req.url] = {html, css, state};
- return cashes[req.headers.host + req.url]
- },
- app,
- template,
- templatePath: path.join(config.output.path, 'index.html'),
- isNotEmbed: true,
- outputPath: config.output.path,
- port: process.env.PORT || 80,
- debug: true
- };
- let port = options.port || 80;
- const appl = Express();
- appl.get('/sitemap.xml', sitemap());
- appl.get('/robots.txt', robots());
- appl.use(createExpressMiddleware(options));
- appl.use(Express.static(config.output.path));
- appl.listen(port, (err)=>{
- if (err) throw err;
- console.log('Listening %s', port);
- });
Add Comment
Please, Sign In to add comment