Skip to content

Instantly share code, notes, and snippets.

@lesniakania
Last active November 3, 2015 17:49
Show Gist options
  • Save lesniakania/e94ae45a5dd99b05fba0 to your computer and use it in GitHub Desktop.
Save lesniakania/e94ae45a5dd99b05fba0 to your computer and use it in GitHub Desktop.
import React from 'react';
import { match, RoutingContext } from 'react-router';
import ReactDOMServer from 'react-dom/server';
import Express from 'express';
import http from 'http';
import Routes from './src/routes';
let app = Express();
let port = process.env.PORT || DefaultConfig.Port;
app.use((req, res) => {
match({ routes: Routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
res.render('index', {
isDevelopment: isDevelopment,
app: ReactDOMServer.renderToString(<RoutingContext {...renderProps} />)
});
} else {
res.status(404).send('Not found');
}
})
});
http.createServer(app).listen(port, function() {
console.log('Express server listening on port ' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment