Skip to content

Commit 5731fb5

Browse files
committed
lesson complete
1 parent 8bc9eba commit 5731fb5

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

lessons/01-setting-up/index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,8 @@ import { render } from 'react-dom'
33
import App from './modules/App'
44
import { Router, Route, hashHistory, IndexRoute, browserHistory} from 'react-router';
55

6-
import About from './modules/About';
7-
import Repos from './modules/Repos';
8-
import Repo from './modules/Repo'
9-
import Home from './modules/Home'
6+
import routes from './modules/routes';
107

11-
render((
12-
<Router history={hashHistory}>
13-
<Route path="/" component={App}>
14-
<IndexRoute component={Home}/>
15-
<Route path="/repos" component={Repos}>
16-
{/* add this new route*/}
17-
<Route path="/repos/:userName/:repoName" component={Repo}/>
18-
</Route>
19-
<Route path="/about" component={About}/>
20-
</Route>
21-
</Router>
22-
), document.getElementById('app'))
8+
render(
9+
<Router routes={routes} history={hashHistory}/>
10+
, document.getElementById('app'))

lessons/01-setting-up/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start" : "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
8-
"start:dev" : "webpack-dev-server --inline --content-base public --history-api-fallback",
9-
"start:prod" : "webpack && node server.js"
10-
},
7+
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
8+
"start:dev": "webpack-dev-server --inline --content-base public/ --history-api-fallback",
9+
"start:prod": "npm run build && node server.bundle.js",
10+
"build:client": "webpack",
11+
"build:server": "webpack --config webpack.server.config.js",
12+
"build": "npm run build:client && npm run build:server"
13+
},
1114
"author": "",
1215
"license": "ISC",
1316
"dependencies": {

lessons/01-setting-up/server.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,48 @@
1-
var express = require('express')
2-
var path = require('path')
3-
var compression = require('compression')
1+
import express from 'express'
2+
import path from 'path'
3+
import compression from 'compression'
4+
import React from 'react'
5+
import { renderToString } from 'react-dom/server'
6+
import { match, RouterContext } from 'react-router'
7+
import routes from './modules/routes'
48

59
var app = express()
610

711
app.use(compression())
812

913
// serve our static stuff like index.css
10-
app.use(express.static(path.join(__dirname, 'public')))
14+
app.use(express.static(path.join(__dirname, 'public'), {index: false}))
1115

12-
app.get('*', function(req, res) {
13-
res.sendFile(path.join(__dirname, 'public', 'index.html'))
16+
// send all requests to index.html so browserHistory works
17+
app.get('*', (req, res) => {
18+
match({ routes, location: req.url }, (err, redirect, props) => {
19+
if (err) {
20+
res.status(500).send(err.message)
21+
} else if (redirect) {
22+
res.redirect(redirect.pathname + redirect.search)
23+
} else if (props) {
24+
// hey we made it!
25+
const appHtml = renderToString(<RouterContext {...props}/>)
26+
res.send(renderPage(appHtml))
27+
} else {
28+
res.status(404).send('Not Found')
29+
}
30+
})
1431
})
1532

16-
var PORT = process.env.PORT || 8080
33+
function renderPage(appHtml) {
34+
return `
35+
<!doctype html public="storage">
36+
<html>
37+
<meta charset=utf-8/>
38+
<title>My First React Router App</title>
39+
<link rel=stylesheet href=/index.css>
40+
<div id=app>${appHtml}</div>
41+
<script src="/bundle.js"></script>
42+
`
43+
}
1744

18-
app.listen(PORT, function(){
19-
console.log('Production Express server running at localhost:' + PORT)
45+
var PORT = process.env.PORT || 8080
46+
app.listen(PORT, function() {
47+
console.log('Production Express server running at localhost:' + PORT)
2048
})

0 commit comments

Comments
 (0)