-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (87 loc) · 3.86 KB
/
server.js
File metadata and controls
106 lines (87 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*-------------------------------------------------------------------------------------------------------------------*\
| Copyright (C) 2016 dejanglozic.com
| Modified from the original react-engine example by PayPal (see react-engine for details) |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| with the License. |
| |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed |
| on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for |
| the specific language governing permissions and limitations under the License. |
\*-------------------------------------------------------------------------------------------------------------------*/
'use strict';
const PORT = process.env.PORT || 3000;
import {join} from 'path';
import express from 'express';
import ReactEngine from 'react-engine';
import items from './items.json';
import routes from './public/routes.jsx';
let app = express();
// create the view engine with `react-engine`
let engine = ReactEngine.server.create({
routes: routes,
routesFilePath: join(__dirname, '/public/routes.jsx'),
performanceCollector: function(stats) {
console.log(stats);
}
});
// set the engine
app.engine('.jsx', engine);
// set the view directory
app.set('views', join(__dirname, '/public/views'));
// set jsx as the view engine
app.set('view engine', 'jsx');
// finally, set the custom view
app.set('view', ReactEngine.expressView);
// expose public folder as static assets
app.use(express.static(join(__dirname, '/public')));
app.get('/api/items', function(req, res) {
setTimeout(function() {
res.json(items);
setTimeout(function() {
emitStatus(items);
}, 1000);
}, 1000);
});
// add our app routes
app.get('*', function(req, res) {
res.render(req.url, {});
});
app.use(function(err, req, res, next) {
console.error(err);
// http://expressjs.com/en/guide/error-handling.html
if (res.headersSent) {
return next(err);
}
if (err._type && err._type === ReactEngine.reactRouterServerErrors.MATCH_REDIRECT) {
return res.redirect(302, err.redirectLocation);
}
else if (err._type && err._type === ReactEngine.reactRouterServerErrors.MATCH_NOT_FOUND) {
return res.status(404).send('Route Not Found!');
}
else {
// for ReactEngine.reactRouterServerErrors.MATCH_INTERNAL_ERROR or
// any other error we just send the error message back
return res.status(500).send(err.message);
}
});
const server = app.listen(PORT, function() {
console.log('Example app listening at port %s', PORT);
});
var io = require('socket.io')(server);
function emitStatus(items) {
var status = [];
for (var i in items) {
var item = items[i];
var s = {};
s.status = (Math.random()<.5)?"active":"error";
s.id = item.id;
setTimeout(function(status) {
io.emit("status", [status]);
}, (Math.floor((Math.random() * 900) + 100)), s);
}
}