Skip to content
This repository was archived by the owner on Jan 22, 2020. It is now read-only.

Commit 2df2c8d

Browse files
committed
Merge pull request #159 from remarkablemark/master
Update example app with 404 and 500
2 parents d0ed06f + 8fd56aa commit 2df2c8d

File tree

11 files changed

+89
-14
lines changed

11 files changed

+89
-14
lines changed

example/Readme.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This movie catalog app illustrates the usage of react-engine to build and run an
1313
```shell
1414
# cd `into_this_dir`
1515
$ npm install
16-
$ npm start
16+
$ npm start # or `npm run dev`
1717
$ open http://localhost:3000
1818
```
1919

@@ -202,7 +202,7 @@ $ open http://localhost:3000
202202
res.render(req.url, {
203203
movies: require('./movies.json')
204204
});
205-
});
205+
});
206206

207207
// add the error handler middleware
208208
app.use(function(err, req, res, next) {
@@ -224,7 +224,7 @@ $ open http://localhost:3000
224224
// any other error we just send the error message back
225225
return res.status(500).send(err.message);
226226
}
227-
});
227+
});
228228

229229
// the last step in the server side is to configure the express app to listen on port 3000
230230
app.listen(3000, function() {
@@ -243,7 +243,7 @@ $ open http://localhost:3000
243243
# 2. json-loader for webpack to load json files
244244
$ npm install babel-loader json-loader --save
245245

246-
# next, add a webpack configuration file
246+
# next, add a webpack configuration file
247247
$ touch webpack.config.js
248248

249249
# configure webpack to build a bundle.js file using public/index.js as the main file
@@ -299,6 +299,3 @@ $ open http://localhost:3000
299299
$ touch public/styles.css
300300
# copy the contents for this file from http://bit.ly/1m2co1B
301301
```
302-
303-
### to-do
304-
* add support for [404 and 500 pages](https://github.com/samsel/react-engine/tree/9666fb3bf47f29981dbdcb4160445e3efce67b5c/example/old/public/views)

example/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"scripts": {
6-
"start": "webpack && node index.js"
6+
"start": "webpack && node index.js",
7+
"dev": "webpack -w & nodemon index.js"
78
},
89
"author": "Sam Selvanathan ([email protected])",
910
"dependencies": {
@@ -19,5 +20,8 @@
1920
"react-router": "^2.4.0",
2021
"serve-favicon": "^2.3.0",
2122
"webpack": "^1.12.9"
23+
},
24+
"devDependencies": {
25+
"nodemon": "^1.9.2"
2226
}
2327
}

example/public/routes.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import { Router, Route, IndexRoute, Redirect, browserHistory } from 'react-route
2121
import Layout from './views/layout.jsx';
2222
import ListPage from './views/list.jsx';
2323
import DetailPage from './views/detail.jsx';
24+
import Error404 from './views/404.jsx';
2425

2526
module.exports = (
2627
<Router history={browserHistory}>
2728
<Route path='/' component={Layout}>
2829
<IndexRoute component={ListPage} />
2930
<Route path='/movie/:id' component={DetailPage} />
3031
<Redirect from='/gohome' to='/' />
32+
<Route path='*' component={Error404} />
3133
</Route>
3234
</Router>
3335
);

example/public/views/404.jsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*-------------------------------------------------------------------------------------------------------------------*\
2+
| Copyright (C) 2016 PayPal |
3+
| |
4+
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
5+
| with the License. |
6+
| |
7+
| You may obtain a copy of the License at |
8+
| |
9+
| http://www.apache.org/licenses/LICENSE-2.0 |
10+
| |
11+
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
12+
| on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for |
13+
| the specific language governing permissions and limitations under the License. |
14+
\*-------------------------------------------------------------------------------------------------------------------*/
15+
16+
'use strict';
17+
18+
var React = require('react');
19+
var Layout = require('./layout.jsx');
20+
21+
module.exports = React.createClass({
22+
displayName: '404',
23+
24+
render: function render() {
25+
return (
26+
<h1>URL: {this.props.location.pathname} - Not Found(404)</h1>
27+
);
28+
}
29+
});

example/public/views/500.jsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*-------------------------------------------------------------------------------------------------------------------*\
2+
| Copyright (C) 2016 PayPal |
3+
| |
4+
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
5+
| with the License. |
6+
| |
7+
| You may obtain a copy of the License at |
8+
| |
9+
| http://www.apache.org/licenses/LICENSE-2.0 |
10+
| |
11+
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
12+
| on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for |
13+
| the specific language governing permissions and limitations under the License. |
14+
\*-------------------------------------------------------------------------------------------------------------------*/
15+
16+
'use strict';
17+
18+
var React = require('react');
19+
var Layout = require('./layout.jsx');
20+
21+
module.exports = React.createClass({
22+
displayName: '500',
23+
24+
render: function render() {
25+
return (
26+
<div>
27+
<h1>Internal Service Error (500)</h1>
28+
<h3>Error message: {this.props.err.message}</h3>
29+
<code>{this.props.err.stack}</code>
30+
</div>
31+
);
32+
}
33+
});

example/public/views/detail.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
var React = require('react');
1919

2020
module.exports = React.createClass({
21+
displayName: 'Detail',
2122

2223
render: function render() {
2324
var movieId = this.props.params.id;

example/public/views/layout.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
var React = require('react');
1919

2020
module.exports = React.createClass({
21+
displayName: 'Layout',
2122

2223
render: function render() {
23-
2424
return (
2525
<html>
2626
<head>

example/public/views/list.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ var React = require('react');
1919
var Router = require('react-router');
2020

2121
module.exports = React.createClass({
22+
displayName: 'List',
2223

2324
render: function render() {
24-
2525
return (
2626
<div id='list'>
2727
<h1>Movies</h1>

example/server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ app.use(function(err, req, res, next) {
7171
return res.redirect(302, err.redirectLocation);
7272
}
7373
else if (err._type && err._type === ReactEngine.reactRouterServerErrors.MATCH_NOT_FOUND) {
74-
return res.status(404).send('Route Not Found!');
74+
return res.status(404).render(req.url);
7575
}
7676
else {
7777
// for ReactEngine.reactRouterServerErrors.MATCH_INTERNAL_ERROR or
7878
// any other error we just send the error message back
79-
return res.status(500).send(err.message);
79+
return res.status(500).render('500.jsx', {
80+
err: {
81+
message: err.message,
82+
stack: err.stack
83+
}
84+
});
8085
}
8186
});
8287

lib/client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ exports.boot = function boot(options, callback) {
5757
Router = require('react-router');
5858
RouterComponent = Router.Router;
5959
match = Router.match;
60-
browserHistory = Router.browserHistory;
60+
61+
// compatibility for both `react-router` v2 and v1
62+
browserHistory = Router.browserHistory || require('history').createHistory();
6163
} catch (err) {
6264
if (!Router && options.routes) {
6365
throw err;

0 commit comments

Comments
 (0)