-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
134 lines (116 loc) · 3.7 KB
/
server.js
File metadata and controls
134 lines (116 loc) · 3.7 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint no-console: 0 */
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import config from './webpack.config.js';
const isDeveloping = process.env.NODE_ENV !== 'production';
const port = isDeveloping ? 3002 : process.env.PORT;
const falcorExpress = require('falcor-express');
const Router = require('falcor-router');
const bodyParser = require('body-parser');
const _ = require('underscore');
let data = require('./app/falcor_cache');
const app = express();
if (isDeveloping) {
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
});
app.use(bodyParser.urlencoded({extended: false}));
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
function buildRefPathSetResults(resource, index, attrs) {
const attributes = _.isArray(attrs) ? attrs : [attrs];
return _.reduce(attributes, (memo, attr) => {
return memo.concat({
path: [resource, index],
value: data[resource][index]
});
}, []);
}
function buildPathSetResults(resource, index, attrs) {
const attributes = _.isArray(attrs) ? attrs : [attrs];
return _.reduce(attributes, (memo, attr) => {
return memo.concat({
path: [resource, index, attr],
value: data[resource][index][attr]
});
}, []);
}
app.use('/model.json', falcorExpress.dataSourceRoute(function(req, res) {
return new Router([
{
route: "todos.add",
call: (callPath, args) => {
const title = args[0];
const id = _.random(1,10000);
_.extend(data.todosById, {[id]: {title: title, completed: false}});
data.todos.push({$type: 'ref', value: ['todosById', id]});
return [
{
path: ['todos', data.todos.length - 1],
value: {$type: "ref", value: ["todosById", id]}
},
{
path: ['todosLength'],
value: data.todos.length
}
]
}
},
{
route: "todosLength",
get: function(pathSet) {
const [resource, attrs] = pathSet;
return [{
path: ['todosLength'], value: data['todos'].length
}];
}
},
{
route: "todos[{integers:indices}]",
get: function(pathSet) {
const [resource, range, attrs] = pathSet;
return _.flatten(_.map(pathSet.indices, (index) => {
return buildRefPathSetResults(resource, index, attrs);
}));
}
},
{
route: "todosById[{integers:ids}]['title']",
get: function(pathSet) {
const [resource, range, attrs] = pathSet;
return _.flatten(_.map(pathSet.ids, (index) => {
return buildPathSetResults(resource, index, attrs);
}));
}
}
]);
}));
app.get('*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
} else {
app.use(express.static(__dirname + '/dist'));
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
}
app.listen(port, '127.0.0.1', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.', port, port);
});