forked from nikospara/angular-require-lazy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (127 loc) · 5.01 KB
/
app.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
var
fs = require("fs"),
express = require("express"),
app = express(),
options = require("./build-scripts/options-grunt.js"),
shared = require("require-lazy").shared;
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/build'));
app.use(express.static(__dirname + '/WebContent'));
// lazy registry
app.get("/scripts/lazy-registry.js", getLazyRegistry);
// API ////////////////////////////////////////////////////////////////////////
app.post("/api/user/login", express.bodyParser(), function(req, res) {
//console.log(req.body); // works as expected
res.json({id:1, defaultCategoryId:1, preferences:null});
});
app.get("/api/user/:id/categories", function(req, res) {
console.log(req.route.params.id); // works as expected
res.json({
"payload": [
{"key": 1, "name": "Other"},
{"key": 2, "name": "Rent"},
{"key": 3, "name": "Food"},
{"key": 4, "name": "Entertainment"}
]
});
});
app.post("/api/user/:id/categories", singleStringBodyParser, function(req, res) {
//console.log(require("util").inspect(req));
//
// $.ajax("/api/user/1/categories",{dataType:"json",contentType:"application/json",type:"POST",data:"CatName"}).then(function(x){console.log(x);})
//
///////////////////////////////////////////////////////////////////////////
// res.json({
// "payload": {"key": parseInt(Math.random()*1000+1000), "name": req.body}
// });
///////////////////////////////////////////////////////////////////////////
setTimeout(function() { // simulate delay
res.json({
"payload": {"key": parseInt(Math.random()*1000+1000), "name": req.body}
});
}, 3000);
});
app.put("/api/user/:id/categories", express.bodyParser(), function(req, res) {
var c = req.body;
console.log("Changing name of category with key ", c.key, " to ", c.name);
setTimeout(function() { // simulate delay
res.json({
"payload": Math.random() < 0.7 // simulate success with 70% chance
});
}, 1000);
});
app["delete"]("/api/user/:id/categories/:key", express.bodyParser(), function(req, res) {
setTimeout(function() { // simulate delay
res.json({
"payload": true
});
}, 3000);
});
app.post("/api/user/:userid/expenses", express.bodyParser(), function(req, res) {
console.log(req.body);
res.json({id:1});
});
app.get("/api/user/:id/expenses", function(req, res) {
console.log(req.route.params.id + " - " + req.query.year + "/" + req.query.month); // works as expected
res.json({
"payload": [
{key:1, date:"2013-08-07T21:00:01.000Z", amount: 10, reason: "One", categoryId: 1, special: null},
{key:2, date:"2013-08-08T21:00:02.000Z", amount: 12, reason: "Two", categoryId: 2, special: null},
{key:3, date:"2013-08-08T21:00:03.000Z", amount: 14, reason: "Three", categoryId: 3, special: null},
{key:4, date:"2013-08-08T21:00:04.000Z", amount: 15, reason: "Four", categoryId: 1, special: null},
{key:5, date:"2013-08-08T21:00:05.000Z", amount: 18, reason: "Five", categoryId: 1, special: null},
{key:11, date:"2013-08-08T21:00:11.000Z", amount: 10, reason: "One", categoryId: 2, special: null},
{key:12, date:"2013-08-08T21:00:12.000Z", amount: 12, reason: "Two", categoryId: 2, special: null},
{key:13, date:"2013-08-08T21:00:13.000Z", amount: 14, reason: "Three", categoryId: 1, special: null},
{key:14, date:"2013-08-08T21:00:14.000Z", amount: 15, reason: "Four", categoryId: 1, special: null},
{key:15, date:"2013-08-08T21:00:15.000Z", amount: 18, reason: "Five", categoryId: 1, special: null},
{key:21, date:"2013-08-08T21:00:21.000Z", amount: 10, reason: "One", categoryId: 3, special: null},
{key:22, date:"2013-08-08T21:00:22.000Z", amount: 12, reason: "Two", categoryId: 1, special: null},
{key:23, date:"2013-08-08T21:00:23.000Z", amount: 14, reason: "Three", categoryId: 1, special: null},
{key:24, date:"2013-08-08T21:00:24.000Z", amount: 15, reason: "Four", categoryId: 1, special: null},
{key:25, date:"2013-08-09T21:00:25.000Z", amount: 18, reason: "Five", categoryId: 2, special: null}
]
});
});
///////////////////////////////////////////////////////////////////////////////
app.listen(8110);
console.log("Server running at http://localhost:8110 - get app.html or app-built.html");
function getLazyRegistry(req, res) {
var modules, text, i, metadata, pmresult;
modules = options.discoverModules();
pmresult = makePmresult(modules);
text = shared.createModulesRegistryText(pmresult, options, {
inludeModuleName: false,
generateBody: true,
nullBundleDeps: true,
writeBundleRegistrations: false
});
res.type("application/javascript");
res.send(text);
function makePmresult(modules) {
var i, dummyParents = ["DUMMY"], pmresult = {
modulesArray: []
};
for( i=0; i < modules.length; i++ ) {
pmresult.modulesArray.push({
name: modules[i],
index: i,
parents: dummyParents
});
}
return pmresult;
}
}
function singleStringBodyParser(req, res, next) {
var buf = "";
req.setEncoding("utf8");
req.on("data", onData);
req.on("end", onEnd);
function onData(chunk) {
buf += chunk;
}
function onEnd() {
req.body = buf;
next();
}
}