-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c374c71
commit 525c625
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import express from 'express'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import React from 'react'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import Hello from './Hello.js'; | ||
|
||
function handleRender(req, res) { | ||
const html = ReactDOMServer.renderToString(<Hello />); | ||
|
||
fs.readFile('./index.html', 'utf8', function (err, data) { | ||
if (err) throw err; | ||
|
||
const document = data.replace(/<div id="app"><\/div>/, `<div id="app">${html}</div>`); | ||
|
||
res.send(document); | ||
}); | ||
} | ||
|
||
const app = express(); | ||
|
||
// Serve built files with static files middleware | ||
app.use('/build', express.static(path.join(__dirname, 'build'))); | ||
|
||
// Serve requests with our handleRender function | ||
app.get('*', handleRender); | ||
|
||
// Start server | ||
app.listen(3000); |