-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.js
55 lines (44 loc) · 1.37 KB
/
server.js
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
import express from 'express';
import fs from 'fs';
import { Router } from 'react-router';
import Location from 'react-router/lib/location';
import routes from './src/routes';
import ReactDOMServer from 'react-dom/server';
import React from 'react';
import Iso from 'iso';
import alt from './src/alt';
let app = express();
app.use(express.static('public'));
// Data endpoints
app.get('/orders.json', (req, res) => {
res.sendFile(__dirname + '/data/orders.json');
});
app.get('/sales_stats.json', (req, res) => {
res.sendFile(__dirname + '/data/sales_stats.json');
});
app.get('/orders', (req, res, next) => {
const orders = JSON.parse(fs.readFileSync(__dirname + '/data/orders.json'));
res.locals.data = {
OrdersStore: {
orders: orders,
selectedStatus: 'all',
amountFilter: null
}
};
next();
});
// Render UI
app.use((req, res, next) => {
alt.bootstrap(JSON.stringify(res.locals.data || {}));
let iso = new Iso();
Router.run(routes, new Location(req.url), (error, props) => {
const content = ReactDOMServer.renderToString(<Router {...props} />);
iso.add(content, alt.flush());
res.render('index.ejs', { html: iso.render() });
});
});
const server = app.listen(8000, () => {
const host = server.address().address;
const port = server.address().port;
console.log('Storekeeper app listening at http://%s:%s', host, port);
});