-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-server.js
149 lines (135 loc) · 3.88 KB
/
local-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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var express = require('express');
var app = express();
var fs = require("fs");
var { mapValues } = require("lodash");
var bp = require("body-parser");
var cors = require("cors");
app.use(bp.json());
app.use(cors());
var timer = null;
var cmTimer = null;
var timeout = 250;
var db = {d: { results: JSON.parse(fs.readFileSync(__dirname + "/db.json", { charset: "utf8" })) } };
var db_ = db.d.results.reduce(function ( obj, item ) {
obj[item.ID] = { d: item };
return obj;
}, {});
// GET LIST
app.get("/items", function ( req, res ) {
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout(function () {
res.send(db);
}, timeout);
});
// DO THE CONTEXT THINGY
app.post("/Pages/content/_api/contextinfo", function ( req, res ) {
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout(function () {
res.send({
d: {
GetContextWebInformation: {
FormDigestValue: "1234"
}
}
});
}, timeout);
});
// OPTIONS
app.get("/opt", function ( req, res ) {
if ( cmTimer ) {
clearTimeout(cmTimer);
}
cmTimer = setTimeout(function () {
res.send({
d: {
results: [
{
Variable: "hideSearchWhileEditing",
Value: true
}, {
Variable: "images",
Value: "https://jsbin-user-assets.s3.amazonaws.com/dhenson02"
}, {
Variable: "scrollOnNav",
Value: false
}, {
Variable: "resetOpenOnNav",
Value: false
}
]
}
});
}, timeout);
});
// GET ITEM
app.get("/items(:id)", function ( req, res ) {
var id = req.params.id.slice(1,-1);
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout(function () {
res.send(db_[id]);
}, timeout);
});
// SAVE ITEM
app.post("/items(:id)", function ( req, res ) {
var id = req.params.id.slice(1, -1);
var data = mapValues(req.body, function ( val, key ) {
if ( key === "__metadata" ) {
return db_[id].d.__metadata;
}
return ( Array.isArray(val) ) ? { results: val } : val;
});
data.ID = id;
data.Id = id;
db_[id].d = Object.assign({}, db_[id].d, data);
db.d.results = db.d.results.map(function ( item ) {
if ( item.ID === id ) {
item = db_[id].d;
}
return item;
});
fs.writeFileSync(__dirname + "/db.json", JSON.stringify(db.d.results), { charset: "utf8" });
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout(function () {
res.send({ status: "success" });
}, timeout);
});
// CREATE ITEM
app.post('/items', function ( req, res ) {
var id = Number(Object.keys(db_).sort(function ( x, y ) { return x - y; }).pop()) + 1;
var data = mapValues(req.body, function ( val ) {
return ( Array.isArray(val) ) ? { results: val } : val;
});
data.ID = id;
data.Id = id;
db.d.results.push(data);
db_[id] = { d: data };
fs.writeFileSync(__dirname + "/db.json", JSON.stringify(db.d.results), { charset: "utf8" });
if ( timer ) {
clearTimeout(timer);
}
timer = setTimeout(function () {
res.send({ status: "success" });
}, timeout);
});
// IS CONTENT MANAGER?
app.get("/check/true", function ( req, res ) {
if ( cmTimer ) {
clearTimeout(cmTimer);
}
cmTimer = setTimeout(function () {
res.send({ status: "success" });
}, timeout);
});
var server = app.listen(3001, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Local development server listening at http://%s:%s', host, port);
});