/
0018_cleaner_rest_api.htm
399 lines (301 loc) · 14 KB
/
0018_cleaner_rest_api.htm
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>The 3D Web Coder</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="3dwc.css"/>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js" type="text/javascript"></script>
</head>
<body>
<!--
#3dwebcoder #swig #handlebars #nodejs #expressjs #webgl #3dweb #heroku
akn_include
-->
<h3>Cleaner Node Server REST API Implementation</h3>
<p>My colleague Cyrille Fauvel provided some suggestions for me to apply various best practices to my
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/node-server-rendering-using-either-swig-and-handlebars.html#4">
Node.js WebGL viewer server implementation</a> and its
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/node-server-rendering-using-either-swig-and-handlebars.html#5">
interactive testing framework</a>:</p>
<ul>
<li><a href="#2">Criteria to choose the rendering engine?</a></li>
<!--
<li><a href="#3">Strictly separate content, styles and logic</a></li>
<li><a href="#4">Avoid browser specific JavaScript</a></li>
<li><a href="#6">Use window.location.host to define the base URL</a></li>
<li><a href="#8">Interactive testing framework</a></li>
-->
<li><a href="#5">Eliminate explicit request method checking</a></li>
<li><a href="#7">Complete node.js server implementation</a></li>
<li><a href="#9">Download</a></li>
</ul>
<a name="2"></a>
<h4>Criteria to choose the rendering engine</h4>
<p><strong>Question:</strong> You
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/post-data-and-swig-driven-webgl-viewer-node-server.html#3">
listed a couple of different rendering engines</a>,
then picked Swig, not EJS.
Can you explain why?
I use EJS myself.</p>
<p><strong>Answer:</strong>
I picked the ones that looked smallest and easiest to get started with.
I ended up implementing
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/node-server-rendering-using-either-swig-and-handlebars.html#3">
support for both Swig and Handlebars</a>, with hardly any differences.</p>
<p>Presumably I could add support for EJS equally easily.
I just need to find time to take a look at it.</p>
<!--
<a name="3"></a>
<h4>Strictly Separate Content, Styles and Logic</h4>
<p><strong>Suggestion:</strong> For
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/post-data-and-swig-driven-webgl-viewer-node-server.html#5">
transferring data to the view template</a>,
you "added an <code>onload</code> attribute to the <code>body</code> tag to call the <code>start_render</code> JavaScript function."</p>
<p>You are thus mingling the HTML content and the JavaScript logic of your app.</p>
<p>For best practice, you should always strictly separate content, styles and logic, e.g., HTML, CSS and JavaScript.</p>
<p>Eliminate all style definitions and JavaScript code from the HTML.</p>
<p>Always use <code>class</code> attributes referring CSS style instead of explicit <code>style</code> attributes on the HTML elements.</p>
<p>Instead of adding code to the <code>onload</code> attribute to the <code>body</code> tag, define a jquery document ready handler:</p>
<pre class="prettyprint">
$(document).ready (function () { ... }) ;
</pre>
<a name="4"></a>
<h4>Avoid Browser Specific JavaScript</h4>
<p><strong>Suggestion:</strong> <code>document.getElementById('p').innerHTML;</code>
is not cross platform.
It will only work in certain browsers, e.g. Chrome.</p>
<p>The jquery method <code>$(‘#p).text()</code> is more portable.</p>
-->
<a name="5"></a>
<h4>Eliminate explicit request method checking</h4>
<p><strong>Suggestion:</strong>
I like your incremental API implementation with separate handlers for the routes <code>api/v1</code> and <code>api/v2</code>.</p>
<p>The
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/post-data-and-swig-driven-webgl-viewer-node-server.html#6">
implementation of <code>routes/api.js</code></a> is
not so nice, though, using if clauses testing the request method to distinguish between GET and POST.</p>
<p>How about implementing a separate API router class doing it like this instead:</p>
<pre class="prettyprint">
var express =require ('express') ;
var router1 =express.Router () ;
router1.use (bodyParser.json ()) ;
router1.get ('/', function(req, res) {
res.send('API v1 GET: Hello World!');
});
router1.post ('/', function(req, res) {
res.send('API v1 POST: '
+ JSON.stringify(req.body));
});
module.exports.v1 =router1 ;
var router2 =express.Router () ;
router2.use (bodyParser.json ()) ;
router2.get ('/', function(req, res) {
res.send('API v2 GET: Here belongs a succinct '
+ 'explanation how to use The Building Coder '
+ 'WebGL Viewer REST API v2...');
});
Router2.post ('/', function(req, res) {
console.log('API v2 POST: ' + JSON.stringify(req.body));
res.render('viewer', req.body);
});
module.exports.v2 =router2 ;
</pre>
<p>I much prefer this to lots of if clauses which make things hard to read and maintain.</p>
<!--
<a name="6"></a>
<h4>Use window.location.host to Define the Base URL</h4>
<p><strong>Question:</strong> You implemented a Boolean variable and JavaScript helper functions to define the base URL and switch between Heroku and the local host.</p>
<p>You can significantly simplify this by using <code>window.location.host</code> instead.</p>
<p>Setting the base URL to <code>'http://' + window.location.host</code> will work for both local and global URLs, independently of the server, port and everything.</p>
<pre class="prettyprint">
function get_base_url_0016() {
return 'http://' + window.location.host;
}
</pre>
-->
<a name="7"></a>
<h4>Complete Node.js Server Implementation</h4>
<p>Following Cyrille's advice, I reimplemented the API request handler.</p>
<p>The node.js server still consists of the same three main pieces:</p>
<ul>
<li>The server mainline server.js</li>
<li>The REST API implementation routes/api.js</li>
<li>The Swig or Handlebars viewer template</li>
</ul>
<p>I updated the first two of those according to Cyrille's suggestions.</p>
<p>First, I replaced the if clauses testing the request method in the API implementation and simply returned four separate functions instead of two:</p>
<pre class="prettyprint">
exports.v1get = function(req, res) {
res.send('API v1 GET: Hello World!');
}
exports.v1post = function(req, res) {
res.send('API v1 POST: '
+ JSON.stringify(req.body));
}
exports.v2get = function(req, res) {
res.send('API v2 GET: Here belongs a succinct '
+ 'explanation how to use The Building Coder '
+ 'WebGL Viewer REST API v2...');
}
exports.v2post = function(req, res) {
console.log('API v2 POST: ' + JSON.stringify(req.body));
res.render('viewer', req.body);
}
</pre>
<p>Now, in the mainline implementation in server.js, I can provide those four directly to the app get and post functions:</p>
<pre class="prettyprint">
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {}); // leads to public/index.html
var bodyParser = require('body-parser');
app.use( bodyParser.json({ limit: '1mb' }) );
app.use( bodyParser.urlencoded({ extended: true, limit: '1mb' }) );
app.set('views', __dirname + '/views');
var use_swig_render = false;
if( use_swig_render ) {
var swig = require('swig');
app.engine('swig', swig.renderFile);
app.set('view engine', 'swig');
swig.setDefaults({ cache: false });
app.set('view cache', false);
}
else {
var handlebars = require('express-handlebars');
app.engine('handlebars', handlebars());
app.set('view engine', 'handlebars');
}
var api = require('./routes/api');
app.get('/api/v1', api.v1get);
app.post('/api/v1', api.v1post);
app.get('/api/v2', api.v2get);
app.post('/api/v2', api.v2post);
app.listen(app.get('port'), function() {
console.log('Node WebGL app with '
+ (use_swig_render ? 'swig' : 'handlebars')
+ ' is running at localhost:'
+ app.get('port'));
});
</pre>
<p>The entire functionality seen by the outside world remains completely unchanged, so the previous
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/implementing-node-server-http-post-get-vs-post.html#6">
API v1</a> and the enhanced
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/node-server-rendering-using-either-swig-and-handlebars.html#4">
v2</a> continue to run unaffected and can be exercised from the same unmodified
<a href="http://the3dwebcoder.typepad.com/blog/2015/04/node-server-rendering-using-either-swig-and-handlebars.html#5">
interactive testing framework</a>.</p>
<!--
<a name="8"></a>
<h4>Interactive Testing Framework</h4>
<p>I updated the interactive online testing framework for this based on Cyrille's suggestions as well, removing the Boolean variable to toggle between global and local testing and cleanly separating HTML content, CSS styles and JavaScript logic.</p>
<p>Below is the HTML form for testing the current API version <code>v2</code> GET and POST functionality.</p>
<p>It now uses this much simpler approach to define the base URL:</p>
<pre class="prettyprint">
function get_base_url_0018() {
return 'http://' + window.location.host;
}
</pre>
<p>Here it is for you to try out live:</p>
<script>
function get_base_url_0018() {
return 'http://' + window.location.host;
}
function get_api_url_0018() {
var api_route = '/api/v2';
return get_base_url_0018() + api_route;
}
function open_node_server_window_0018() {
var w = 400;
var h = 400;
var features = 'width=' + w.toString()
+ ',height=' + h.toString();
window.open(get_api_url_0018(), 'node_server_0018', features);
}
window.onload = function(e) {
var baseurl = get_base_url_0018();
var apiurl = get_api_url_0018();
document.getElementById('form_0018').action = apiurl;
document.getElementById('iframe_0018').src = baseurl;
}
function set_wall_data_0018(geometry_data_option) {
var arrays_cube = {
position: [ 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1,
-1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1,
-1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1,
1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1,
-1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1],
normal: [ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1],
texcoord: [1, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1,
1, 0, 0, 0, 0, 1, 1, 1],
indices: [0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23]
};
var arrays_nil = {
position: [],
normal: [],
indices: []
};
var arrays_list = [
arrays_cube,
arrays_wall_simple,
arrays_wall_straight,
arrays_wall_curved_facets,
arrays_wall_curved_smooth,
arrays_nil];
var arrays = arrays_list[parseInt(geometry_data_option)];
document.getElementById('position').value = arrays['position'].join(', ');
document.getElementById('normal').value = arrays['normal'].join(', ');
document.getElementById('indices').value = arrays['indices'].join(', ');
}
</script>
<center>
<form method="post" id="form_0018" target="node_server_0018">
<p>Populate with hard-wired wall geometry data:
<select name="walldata" onchange="set_wall_data_0018(this.form.walldata.value)">
<option value="0">Cube</option>
<option value="1">Simple</option>
<option value="2">Straight</option>
<option value="3">Curved Facets</option>
<option value="4">Curved Smooth</option>
<option value="5">Nil – Clear All</option>
</select></p>
<table>
<tr><td colspan="2">... or enter your own data:</td></tr>
<tr><td>position:</td><td><input type="text" id="position" name="position" value="1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1"/></td></tr>
<tr><td>normal:</td><td><input type="text" id="normal" name="normal" value="1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1"/></td></tr>
<tr><td>indices:</td><td><input type="text" id="indices" name="indices" value="0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23"/></td></tr>
<tr><td></td><td>
<input value="Get" type="submit" onclick="this.form.method='get';open_node_server_window_0018()"/>
<input value="Post" type="submit" onclick="this.form.method='post';open_node_server_window_0018('post')"/>
</td></tr>
</table>
</form>
</center>
<p>As always, the original non-API-driven version and the API version v1 still work exactly as before; I am enhancing the REST API step by step while preserving complete upward compatibility and keeping the original server running continuously at the same time:</p>
<center>
<iframe id="iframe_0018" style="width: 300px; height: 400px;"></iframe>
</center>
-->
<a name="9"></a>
<h4>Download</h4>
<p>The complete node server implementation is available from the
<a href="https://github.com/jeremytammik/NodeWebGL">NodeWebGL GitHub repo</a>,
and the version discussed here is
<a href="https://github.com/jeremytammik/NodeWebGL/releases/tag/0.2.7">0.2.7</a>.</p>
</body>
</html>