-
Notifications
You must be signed in to change notification settings - Fork 0
/
Websites-CMS-Platform-Storing-Data.html
506 lines (442 loc) · 35.3 KB
/
Websites-CMS-Platform-Storing-Data.html
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="manifest" href="/manifest.json" />
<link rel="icon" href="/images/icons/icon-152x152.png" />
<!-- theme-color defines the top bar color-->
<meta name="theme-color" content="#575757" />
<meta
http-equiv="X-Content-Security-Policy"
content="default-src 'self'; script-src 'report-sample' 'self' https://app.posthog.com/static/array.js https://www.googletagmanager.com/gtag/js; style-src 'report-sample' 'self' https://fonts.googleapis.com; object-src 'none'; base-uri 'self'; connect-src 'self' https://app.posthog.com; font-src 'self'; frame-src 'self'; img-src 'self'; manifest-src 'self'; media-src 'self'; report-uri https://pauldambra.report-uri.com/r/d/csp/enforce; worker-src 'self';"
/>
<!-- Add to home screen for Safari on iOS-->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<meta name="apple-mobile-web-app-title" content="MiRaNo" />
<link rel="apple-touch-icon" href="/images/icons/icon-152x152.png" />
<!-- Add to home screen for Windows-->
<meta
name="msapplication-TileImage"
content="/images/icons/icon-152x152.png"
/>
<meta name="msapplication-TileColor" content="#575757" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Websites != CMS Platform - Storing Data - Part 1</title>
<link rel="canonical" href="https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html" />
<meta property="og:url" content="https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html" />
<meta property="og:type" content="article" />
<meta property="og:title" content="Websites != CMS Platform - Storing Data - Part 1" />
<meta
property="og:description"
content="storing data using mongo, mongojs and express"
/>
<meta
property="og:image"
content="https://pauldambra.dev/images/cardboard.jpg"
/>
<meta name="twitter:creator" content="@pauldambra" />
<meta property="fb:app_id" content="1029758320473951" />
<meta name="viewport" content="width=device-width" />
<meta
name="description"
content="storing data using mongo, mongojs and express"
/>
<meta property="fb:pages" content="1029758320473951" />
<link
rel="alternate"
type="application/rss+xml"
title="Mindless Rambling Nonsense"
href="https://pauldambra.dev/feed.xml"
/>
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="prefetch" href="/images/cardboard.jpg" />
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="/assets/css/syntax.css">
<meta name="msvalidate.01" content="54691C3C7B863CEE60F0305D6EDFF7A8" />
<meta
name="google-site-verification"
content="hLKEdujpXNQ9PSZWEcQkwxCgL2z1tWxVedeaUmttH7c"
/>
<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('phc_xcwOfYtGDcfv8619HtTWgHEfVMeTqS5YKkBsVEpy1ns', {
api_host:'https://ph.pauldambra.dev',
person_profiles: 'identified_only' // or 'always' to create profiles for anonymous users as well
})
</script>
</head>
<body class="flex flex-col h-screen">
<header class="flex flex-col px-8 pt-4 text-white text-xl" role="banner">
<div class="flex flex-col sm:flex-row">
<div class="no-underline hover:underline">
<a href="/">Mindless Rambling Nonsense</a>
</div>
<div class="flex-grow my-2 sm:m-0"></div>
<div class="flex justify-start items-end flex-col text-sm">
<div class="flex items-center">
<div class="mr-4">Paul D'Ambra</div>
<a href="https://github.com/pauldambra" rel="noopener">
<img
src="/images/GitHub-Mark-Light-32px.png"
alt="pauldambra on github"
width="32"
height="32"
/>
</a>
</div>
<div class="flex items-center">
<div class="mr-4">Fangler</div>
<a href="https://twitter.com/pauldambra" rel="noopener">
<img
src="/images/twitter-32.png"
alt="pauldambra on twitter"
width="32"
height="32"
/>
</a>
</div>
<div class="flex items-center">
<div class="mr-4"></div>
<a rel="me" href="https://mastodon.me.uk/@pauldambra">
</a>
</div>
</div>
</div>
<div class="flex-grow"></div>
<div
class="flex align-middle items-start px-2 py-4 text-white space-x-4 text-lg"
>
<nav role="navigation">
<a class="underline" href="/">Blog Posts</a>
<a class="underline ml-5" href="/weeknotes.html">Week Notes</a>
<a class="underline ml-5" href="/kids-games.html">Kids games</a>
</nav>
</div>
</header>
<main role="main" class="bg-white p-4 w-11/12 m-auto flex-auto flex-grow">
<script type="application/ld+json">
{
"@context":"http://schema.org",
"@type":"BlogPosting",
"headline":"Websites != CMS Platform - Storing Data - Part 1",
"genre":"",
"keywords":"",
"wordCount":"1138",
"url":"https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html",
"datePublished":"2014-04-12",
"author":{
"@type":"Person",
"name":"Paul D'Ambra",
"sameAs":[
"https://twitter.com/pauldambra",
"https://github.com/pauldambra",
"https://plus.google.com/u/0/+PaulDAmbraPlus"
]
},
"publisher":{
"@type": "Organization",
"name": "Paul D'Ambra",
"sameAs": [
"https://twitter.com/pauldambra",
"https://github.com/pauldambra",
"https://plus.google.com/u/0/+PaulDAmbraPlus"
],
"logo": {
"@type": "ImageObject",
"contentUrl": "https://pauldambra.dev/images/logo.png",
"url": "https://pauldambra.dev"
}
},
"image":{
"@type":"ImageObject",
"contentUrl":"https://pauldambra.dev/images/cardboard.jpg",
"url":"https://pauldambra.dev",
"height":"450",
"width":"1000"
},
"mainEntityOfPage":{
"@type":"WebPage",
"@id":"https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html"
},
"articleBody":"This post is part of a series where I'm hoping to prove to myself that building a dynamic website with NodeJS is much more fun than using a CMS platform. See the first post for an explanation of why\n\nPrevious Post\n\nAfter a day writing DDL for a project that has manual schema versioning against MS SQL and is going through a lot of changes I feel honour bound to write a post about storing data in the Omniclopse site.\n\n\n\nI'll be using MongoDB for two reasons.\n\n\n The implicit schema of a NoSQL database is awesome when you're not sure of the final shape of the data.\n Storing a data structure that's almost definitely going to be sent over the wire as JSON as… JSON makes a lot of sense to me.\n\n\nFirst Steps\nAt least for now each view will have its own document in the database (At the moment there's only one view! so why complicate things).\n\nFirst it is necessary to npm install --save mongojs and then require mongojs within the server module.\n\nvar mongojs = require('mongojs');\nvar db = mongojs('omniclopse', ['pages']);\n\n\nHere the variable db connects to a MongoDB database called omniclopse and a collection called pages.\n\nNext the call to the DB to get the home page data is added to the home route.\n\napp.get('/', function(req, res){\n db.pages.findOne({ name: 'home' }, function(err, doc) {\n if (err) {\n res.render('500', {error: err});\n } \n else if (doc) {\n res.render('home', doc);\n } else {\n res.render('404');\n }\n });\n});\n\n\nI think this code is a bit ugly but we'll be coming back to the server later on!\n\nError Pages\nAdding the 404 and 500 pages is straightforward.\n\nFor example:\n\n<div class=\"container bg-danger\">\n\t<h1>404</h1>\n\t<div>Dang! That doesn't seem to exist.</div>\n</div>\n\n\nThere are two cases where the app will need to return a 404.\n\nFirst, when the URL doesn't exist an HTTP status 400 should be returned\n\ndescribe('GET unknown route sends 404 status', function(){\n it('respond with 404 html', function(done){\n request(server)\n .get('/never-exists')\n .set('Accept', 'text/html')\n .expect('Content-Type', /html/)\n .expect(404, done);\n });\n});\n\n\nSecond, when the database has no entry for the page then the HTTP status should be 200 but the page should be a 404.\n\ndescribe('GET known route with no data sends 404 page with 200 status', function(){\n it('respond with 404 html', function(done){\n request(server)\n .get('/')\n .set('Accept', 'text/html')\n .expect('Content-Type', /html/)\n .expect(200, done)\n .end(function(err, res) {\n if (err) return done(err);\n res.text.should.include(\"Dang! That doesn't seem to exist.\");\n done();\n });\n });\n});\n\n\nAh, but…\n…the MongoDB pages collection is empty. Once this collection contains a match for name: home then this test will fail.\n\nRun Tests against a different database instance\nMuch simpler than mocking the DB (and because I couldn't figure out how to mock it without breaking SuperTest) is running against a test copy of the DB. Very little code to write and the best code is the code you (I?) don't write.\n\nThe code to initialise the database becomes\n\nvar dbName = process.env.NODE_ENV === 'test' ? 'omnitest' : 'omniclopse';\nvar db = mongojs(dbName, ['pages']);\n\n\nand in the test spec files\n\nvar server;\n\nbeforeEach(function() {\n process.env.NODE_ENV = 'test'; \n server = require('../server').app;\n});\n\n\nNow Mocha tests all pass and running the site gives\n\n\nAfter adding {name:'home',carouselImages:[],panels:[]} to the pages collection using the terminal and reloading the page\n\n\nAdding an array of carousel images to the home document:\n\ndb.pages.update({name: 'home' },\n {$set: {\n carouselImages: [\n {\n url:'http://www.fillmurray.com/900/300',\n alt:'Bill Murray',\n caption:'Bill Murray'\n },\n {\n url:'http://www.placecage.com/900/300',\n alt:'Nick Cage',\n caption:'Nick Cage'\n },\n {\n url:'http://www.nicenicejpg.com/900/300',\n alt:'Vanilla Ice',\n caption:'Vanilla Ice'\n }\n ]\n }\n })\n\n\nresults in:\n\n\nAdding an array of panels to the home document results in the desired home page:\n\n\nE Voila\nVery little code, very little effort and the page data is being loaded from the database. Hurrah!\n\nNext\nI'll be adding authentication so that we can then allow an admin user at Omniclopse HQ to change and add data\n"
}
</script>
<article>
<header class="flex flex-col border-b-black border-b-2 bg-white text-black">
<div class="heading">
<div class="date">Sat Apr 12 2014</div>
<h1 class="title leading-10 pt-2 mb-0 mt-1">Websites != CMS Platform - Storing Data - Part 1</h1>
</div>
<div class="meta flex-grow flex flex-row">
<div class="share-this flex self-end space-x-2">
<a
id="facebook-share-link"
class="social-share"
target="_blank"
rel="noopener"
href="https://www.facebook.com/dialog/share?app_id=305449093152216&href=https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html"
>
<img
class="w-8"
src="/images/facebook-black-32.png"
alt="share on facebook"
width="32"
height="32"
/>
</a>
<a
id="twitter-share-link"
class="social-share"
target="_blank"
rel="noopener"
href="https://twitter.com/intent/tweet?text=Websites+%21%3D+CMS+Platform+-+Storing+Data+-+Part+1&via=pauldambra&url=https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html"
>
<img
class="w-8"
src="/images/twitter-black-32.png"
alt="share on twitter"
width="32"
height="32"
/>
</a>
</div>
<div class="more-like-this text-right content-end flex-grow">
<a
class="post-metadata"
href="/categories.html#cms"
>
in: cms
</a>
<div>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#nosql">
nosql
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#learning">
learning
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#mongodb">
mongodb
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#cms">
cms
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#design">
design
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#web">
web
</a>
</span>
<span class="text-slate-500">
<img class="w-6 h-6 inline-block" src="/images/tag.svg" alt="tag-icon" />
<a class="no-underline hover:underline" href="/tags.html#series">
series
</a>
</span>
</div>
</div>
</div>
</header>
<div class="post">
<p>This post is part of a series where I'm hoping to prove to myself that building a dynamic website with NodeJS is much more fun than using a CMS platform. <a href="/2014/02/websites-cms.html">See the first post for an explanation of why</a></p>
<p><a href="/2014/03/testing-with-browserstack-and-selenium.html">Previous Post</a></p>
<p>After a day writing DDL for a project that has manual schema versioning against MS SQL and is going through a lot of changes I feel honour bound to write a post about storing data in the Omniclopse site.</p>
<!--more-->
<p>I'll be using MongoDB for two reasons.</p>
<ol>
<li>The implicit schema of a NoSQL database is awesome when you're not sure of the final shape of the data.</li>
<li>Storing a data structure that's almost definitely going to be sent over the wire as JSON as… JSON makes a lot of sense to me.</li>
</ol>
<h1 id="first-steps">First Steps</h1>
<p>At least for now each view will have its own document in the database (At the moment there's only one view! so why complicate things).</p>
<p>First it is necessary to <code class="language-plaintext highlighter-rouge">npm install --save mongojs</code> and then require mongojs within the server module.</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">mongojs</span> <span class="o">=</span> <span class="nf">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">mongojs</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">db</span> <span class="o">=</span> <span class="nf">mongojs</span><span class="p">(</span><span class="dl">'</span><span class="s1">omniclopse</span><span class="dl">'</span><span class="p">,</span> <span class="p">[</span><span class="dl">'</span><span class="s1">pages</span><span class="dl">'</span><span class="p">]);</span>
</code></pre></div></div>
<p>Here the variable <code class="language-plaintext highlighter-rouge">db</code> connects to a MongoDB database called omniclopse and a collection called pages.</p>
<p>Next the call to the DB to get the home page data is added to the home route.</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">app</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">){</span>
<span class="nx">db</span><span class="p">.</span><span class="nx">pages</span><span class="p">.</span><span class="nf">findOne</span><span class="p">({</span> <span class="na">name</span><span class="p">:</span> <span class="dl">'</span><span class="s1">home</span><span class="dl">'</span> <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">res</span><span class="p">.</span><span class="nf">render</span><span class="p">(</span><span class="dl">'</span><span class="s1">500</span><span class="dl">'</span><span class="p">,</span> <span class="p">{</span><span class="na">error</span><span class="p">:</span> <span class="nx">err</span><span class="p">});</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if </span><span class="p">(</span><span class="nx">doc</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">res</span><span class="p">.</span><span class="nf">render</span><span class="p">(</span><span class="dl">'</span><span class="s1">home</span><span class="dl">'</span><span class="p">,</span> <span class="nx">doc</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">res</span><span class="p">.</span><span class="nf">render</span><span class="p">(</span><span class="dl">'</span><span class="s1">404</span><span class="dl">'</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">});</span>
</code></pre></div></div>
<p>I think this code is a bit ugly but we'll be coming back to the server later on!</p>
<h1 id="error-pages">Error Pages</h1>
<p>Adding the 404 and 500 pages is straightforward.</p>
<p>For example:</p>
<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><div</span> <span class="na">class=</span><span class="s">"container bg-danger"</span><span class="nt">></span>
<span class="nt"><h1></span>404<span class="nt"></h1></span>
<span class="nt"><div></span>Dang! That doesn't seem to exist.<span class="nt"></div></span>
<span class="nt"></div></span>
</code></pre></div></div>
<p>There are two cases where the app will need to return a 404.</p>
<p>First, when the URL doesn't exist an HTTP status 400 should be returned</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nf">describe</span><span class="p">(</span><span class="dl">'</span><span class="s1">GET unknown route sends 404 status</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span>
<span class="nf">it</span><span class="p">(</span><span class="dl">'</span><span class="s1">respond with 404 html</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">done</span><span class="p">){</span>
<span class="nf">request</span><span class="p">(</span><span class="nx">server</span><span class="p">)</span>
<span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">/never-exists</span><span class="dl">'</span><span class="p">)</span>
<span class="p">.</span><span class="nf">set</span><span class="p">(</span><span class="dl">'</span><span class="s1">Accept</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">text/html</span><span class="dl">'</span><span class="p">)</span>
<span class="p">.</span><span class="nf">expect</span><span class="p">(</span><span class="dl">'</span><span class="s1">Content-Type</span><span class="dl">'</span><span class="p">,</span> <span class="sr">/html/</span><span class="p">)</span>
<span class="p">.</span><span class="nf">expect</span><span class="p">(</span><span class="mi">404</span><span class="p">,</span> <span class="nx">done</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span>
</code></pre></div></div>
<p>Second, when the database has no entry for the page then the HTTP status should be 200 but the page should be a 404.</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nf">describe</span><span class="p">(</span><span class="dl">'</span><span class="s1">GET known route with no data sends 404 page with 200 status</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span>
<span class="nf">it</span><span class="p">(</span><span class="dl">'</span><span class="s1">respond with 404 html</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">done</span><span class="p">){</span>
<span class="nf">request</span><span class="p">(</span><span class="nx">server</span><span class="p">)</span>
<span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">)</span>
<span class="p">.</span><span class="nf">set</span><span class="p">(</span><span class="dl">'</span><span class="s1">Accept</span><span class="dl">'</span><span class="p">,</span> <span class="dl">'</span><span class="s1">text/html</span><span class="dl">'</span><span class="p">)</span>
<span class="p">.</span><span class="nf">expect</span><span class="p">(</span><span class="dl">'</span><span class="s1">Content-Type</span><span class="dl">'</span><span class="p">,</span> <span class="sr">/html/</span><span class="p">)</span>
<span class="p">.</span><span class="nf">expect</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="nx">done</span><span class="p">)</span>
<span class="p">.</span><span class="nf">end</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">return</span> <span class="nf">done</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
<span class="nx">res</span><span class="p">.</span><span class="nx">text</span><span class="p">.</span><span class="nx">should</span><span class="p">.</span><span class="nf">include</span><span class="p">(</span><span class="dl">"</span><span class="s2">Dang! That doesn't seem to exist.</span><span class="dl">"</span><span class="p">);</span>
<span class="nf">done</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="p">});</span>
</code></pre></div></div>
<h2 id="ah-but">Ah, but…</h2>
<p>…the MongoDB pages collection is empty. Once this collection contains a match for <code class="language-plaintext highlighter-rouge">name: home</code> then this test will fail.</p>
<h1 id="run-tests-against-a-different-database-instance">Run Tests against a different database instance</h1>
<p>Much simpler than mocking the DB (and because I couldn't figure out how to mock it without breaking SuperTest) is running against a test copy of the DB. Very little code to write and the best code is the code you (I?) don't write.</p>
<p>The code to initialise the database becomes</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">dbName</span> <span class="o">=</span> <span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">NODE_ENV</span> <span class="o">===</span> <span class="dl">'</span><span class="s1">test</span><span class="dl">'</span> <span class="p">?</span> <span class="dl">'</span><span class="s1">omnitest</span><span class="dl">'</span> <span class="p">:</span> <span class="dl">'</span><span class="s1">omniclopse</span><span class="dl">'</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">db</span> <span class="o">=</span> <span class="nf">mongojs</span><span class="p">(</span><span class="nx">dbName</span><span class="p">,</span> <span class="p">[</span><span class="dl">'</span><span class="s1">pages</span><span class="dl">'</span><span class="p">]);</span>
</code></pre></div></div>
<p>and in the test spec files</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">var</span> <span class="nx">server</span><span class="p">;</span>
<span class="nf">beforeEach</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">process</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">NODE_ENV</span> <span class="o">=</span> <span class="dl">'</span><span class="s1">test</span><span class="dl">'</span><span class="p">;</span>
<span class="nx">server</span> <span class="o">=</span> <span class="nf">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">../server</span><span class="dl">'</span><span class="p">).</span><span class="nx">app</span><span class="p">;</span>
<span class="p">});</span>
</code></pre></div></div>
<p>Now Mocha tests all pass and running the site gives</p>
<p><img src="/images/home404.png" alt="404 page" class="img-responsive img-thumbnail" /></p>
<p>After adding <code class="language-plaintext highlighter-rouge">{name:'home',carouselImages:[],panels:[]}</code> to the pages collection using the terminal and reloading the page</p>
<p><img src="/images/homeBare.png" alt="empty page" class="img-responsive img-thumbnail" /></p>
<p>Adding an array of carousel images to the home document:</p>
<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="err">db.pages.update(</span><span class="p">{</span><span class="err">name:</span><span class="w"> </span><span class="err">'home'</span><span class="w"> </span><span class="p">}</span><span class="err">,</span><span class="w">
</span><span class="p">{</span><span class="err">$set:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="err">carouselImages:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="err">url:'http://www.fillmurray.com/</span><span class="mi">900</span><span class="err">/</span><span class="mi">300</span><span class="err">'</span><span class="p">,</span><span class="w">
</span><span class="err">alt:'Bill</span><span class="w"> </span><span class="err">Murray'</span><span class="p">,</span><span class="w">
</span><span class="err">caption:'Bill</span><span class="w"> </span><span class="err">Murray'</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="err">url:'http://www.placecage.com/</span><span class="mi">900</span><span class="err">/</span><span class="mi">300</span><span class="err">'</span><span class="p">,</span><span class="w">
</span><span class="err">alt:'Nick</span><span class="w"> </span><span class="err">Cage'</span><span class="p">,</span><span class="w">
</span><span class="err">caption:'Nick</span><span class="w"> </span><span class="err">Cage'</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="err">url:'http://www.nicenicejpg.com/</span><span class="mi">900</span><span class="err">/</span><span class="mi">300</span><span class="err">'</span><span class="p">,</span><span class="w">
</span><span class="err">alt:'Vanilla</span><span class="w"> </span><span class="err">Ice'</span><span class="p">,</span><span class="w">
</span><span class="err">caption:'Vanilla</span><span class="w"> </span><span class="err">Ice'</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="err">)</span><span class="w">
</span></code></pre></div></div>
<p>results in:</p>
<p><img src="/images/homeCarousel.png" alt="partial page" class="img-responsive img-thumbnail" /></p>
<p>Adding an array of panels to the home document results in the desired home page:</p>
<p><img src="/images/homeFull.png" alt="full page" class="img-responsive img-thumbnail" /></p>
<h1 id="e-voila">E Voila</h1>
<p>Very little code, very little effort and the page data is being loaded from the database. Hurrah!</p>
<h1 id="next">Next</h1>
<p>I'll be adding authentication so that we can then allow an admin user at Omniclopse HQ to change and add data</p>
</div>
<div class="mt-8">
<h1>More like this...</h1>
<div class="grid grid-cols-3 gap-4"> <a href="/wrapping_up.html">
<div class="article-tile inline-block w-full border p-4 text-ellipsis align-top relative">
<h3>
Websites != CMS Platform - Wrapping Up
</h3>
<small>
03 Aug 2014
</small>
</div>
</a> <a href="/better-affordance-js.html">
<div class="article-tile inline-block w-full border p-4 text-ellipsis align-top relative">
<h3>
Websites != CMS Platform - Better Editable Affordance with JS for great good
</h3>
<small>
30 Jul 2014
</small>
</div>
</a> <a href="/better-affordance.html">
<div class="article-tile inline-block w-full border p-4 text-ellipsis align-top relative">
<h3>
Websites != CMS Platform - Better Editable Affordance
</h3>
<small>
20 Jul 2014
</small>
</div>
</a> </div>
</div>
</article>
</main>
<footer class="w-full h-4 bg-black text-white py-8 px-4">
<a class="no-underline text-white" href="https://pauldambra.dev/feed.xml">
<img class="inline w-6 h-6" src="/images/rss.svg" alt="the rss feed" />
<span>Subscribe to RSS feed</span>
</a>
</footer>
<script>
var tsl = document.getElementById("twitter-share-link");
tsl.addEventListener("click", function () {
ga("send", {
hitType: "social",
socialNetwork: "twitter",
socialAction: "tweet",
socialTarget: "https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html",
});
});
var fsl = document.getElementById("facebook-share-link");
fsl.addEventListener("click", function () {
ga("send", {
hitType: "social",
socialNetwork: "facebook",
socialAction: "share",
socialTarget: "https://pauldambra.dev/Websites-CMS-Platform-Storing-Data.html",
});
});
</script>
<script defer src="/register-service-worker.js"></script>
<link
href="https://fonts.googleapis.com/css?family=Khula&display=swap"
rel="stylesheet"
/>
</body>
</html>