forked from hafizio/fullstack-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (62 loc) · 1.86 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
55
56
57
58
59
60
61
62
63
64
65
var FalcorServer = require('falcor-express'),
bodyParser = require('body-parser'),
express = require('express'),
Router = require('falcor-router'),
app = express(),
data = {
names: [
{name: 'a'},
{name: 'b'},
{name: 'c'}
]
},
NamesRouter = Router.createClass([
{
route: 'names[{integers:nameIndexes}]["name"]',
get: (pathSet) => {
var results = [];
pathSet.nameIndexes.forEach(nameIndex => {
if (data.names.length > nameIndex) {
results.push({
path: ['names', nameIndex, 'name'],
value: data.names[nameIndex].name
})
}
})
return results
}
},
{
route: 'names.length',
get: () => {
return {path: ['names', 'length'], value: data.names.length}
}
},
{
route: 'names.add',
call: (callPath, args) => {
var newName = args[0];
data.names.push({name: newName})
return [
{
path: ['names', data.names.length-1, 'name'],
value: newName
},
{
path: ['names', 'length'],
value: data.names.length
}
]
}
}
])
app.use(bodyParser.urlencoded({extended: false}));
app.use('/model.json', FalcorServer.dataSourceRoute(() => new NamesRouter()))
app.use(express.static('.'))
app.listen(9090, err => {
if (err) {
console.error(err)
return
}
console.log('navigate to http://localhost:9090')
});