-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
886 lines (781 loc) · 42.6 KB
/
index.html
File metadata and controls
886 lines (781 loc) · 42.6 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Site.js</title>
<!-- Don’t have the browser request the favicon. -->
<link rel='icon' href='data:,'>
<!-- Start: styles -->
<link rel='stylesheet' href='css/highlight.js/github.css'>
<link rel='stylesheet' href='css/index.css'>
<!-- End: styles -->
</head>
<body>
<!-- Used to display a status message on platforms that support the -->
<!-- click-to-copy code progressive enhancement. -->
<div id='copy-alert' class='hidden'><p>Copied!</p></div>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Header -->
<!-- -->
<!------------------------------------------------------------------------------------>
<header>
<!-- Title -->
<h1><strong>Site</strong>.js</h1>
<h2>Small Web construction set.</h2>
<img src='/images/illustrations/person.svg' id='header-image' alt=''>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Navigation -->
<!-- -->
<!------------------------------------------------------------------------------------>
<nav>
<ul>
<li class='selected'>Home</li>
<li><a href='https://github.com/small-tech/site.js'>Source</a></li>
<li><a href='https://github.com/small-tech/site.js/blob/master/README.md'>Docs</a></li>
<li><a href='https://github.com/small-tech/site.js/issues'>Issues</a></li>
<li><a href='https://small-tech.org/fund-us'>Fund Us!</a></li>
</ul>
</nav>
</header>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Main -->
<!-- -->
<!------------------------------------------------------------------------------------>
<main>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Small Web -->
<!-- -->
<!------------------------------------------------------------------------------------>
<aside id='news'>
<h3><strong>New:</strong> Site.js ❤️ Owncast</h3>
<img src='/images/other/owncast-splash.png' alt='Owncast running on a laptop and a phone. The screens show Gabe’s Owncast Server. He’s playing a track called The Second Wave: Fallen Star (original mix). There are chat messages filling up one side of the screen.'>
<p>Site.js is now the easiest way to set up and run your own <a href='https://owncast.online'>Owncast</a> instance.</p>
<p><strong>On your production server, run:</strong></p>
<pre><code>site enable --owncast</code></pre>
<p><em>That’s it!</em></p>
<p>This will <a href='https://owncast.online/quickstart/'>install Owncast</a>, <a href='https://owncast.online/docs/systemservice/'>set it up as a systemd service</a>, and <a href='https://owncast.online/docs/sslproxies/'>serve it securely</a> at your hostname.</p>
<p>As usual, your <a href='https://letsencrypt.org/'>Let’s Encrypt</a> certificates will be automatically provisioned when you first hit your server and get renewed automatically for you from there on.</p>
<hr>
<p><small>Note that while Site.js will get automatic updates, Owncast will not. However, newer versions of Site.js will always include the latest Owncast install script. Currently, to update Owncast, simply disable your server using <code>site disable</code> and re-enable it using the command above.</small></p>
</aside>
<section id='small-web'>
<h2><strong>The Small Web is for people</strong> <small>(not startups, enterprises, or governments).</small></h2>
<ul id='small-web-details'>
<li>
<h3>One person.</h3>
<img src='/images/illustrations/one-person.svg' alt=''>
<p>
Develop and test on <strong>your own device</strong>.
</p>
</li>
<li>
<h3>One server.</h3>
<img src='/images/illustrations/one-server.svg' alt=''>
<p>
Sync and deploy to <strong>your own VPS</strong>.
</p>
</li>
<li>
<h3>One site.</h3>
<img src='/images/illustrations/one-site.svg' alt=''>
<p>
Host <strong>your own site</strong> at <strong>your own domain</strong>.
</p>
</li>
</ul>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Highlights -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='features' class='highlights'>
<h2><strong>Develop, test, sync, and deploy</strong> <small>(using a single tool that comes in a single binary).</small></h2>
<ul>
<li>
<h3>Just works.</h3>
<p>
No configuration; <a href='#get-started'>get started in seconds</a> on <a href='https://source.small-tech.org/site.js/app#system-requirements'>Linux, macOS, and Windows</a>.
</p>
</li>
<li>
<h3>Free as in freedom.</h3>
<p>
And small as in <a href='https://small-tech.org/about/#small-technology'>small tech</a>.
</p>
</li>
<li>
<h3>Secure by default.</h3>
<p>
Automatic TLS everywhere with <a href='https://github.com/small-tech/auto-encrypt'>Let’s Encrypt</a> and <a href='https://github.com/small-tech/auto-encrypt-localhost'>mkcert</a>.
</p>
</li>
<li>
<h3>For static sites.</h3>
<p>
Write plain HTML, CSS, JS or use the bundled <a href='#static-sites'>Hugo static site generator</a>.
</p>
</li>
<li>
<h3>For dynamic sites.</h3>
<p>
Use simple <a href='#dynamic-sites'>DotJS</a> or <a href='https://github.com/small-tech/site.js#advanced-routing-routesjs-file'>the full power of Node.js and Express.js</a>.
</p>
</li>
<li>
<h3>And more…</h3>
<p>
<a href='#websockets'>WebSockets</a>, <a href='#database'>database</a>, <a href='#proxy-servers'>proxy</a>, live reload, sync, auto-update, statistics, etc.
</p>
</li>
</ul>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Get started -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='get-started'>
<h2>Get started in seconds</h2>
<ul id='platform-tabs' class='hidden'>
<li><button data-platform='linux'>Linux</button></li>
<li><button data-platform='macos'>macOS</button></li>
<li><button data-platform='windows'>Windows</button></li>
</ul>
<ol id='install'>
<li>
<h3>Install Site.js.</h3>
<p>
Launch <span class='linux macos'>a terminal window</span> <span class='no-js'>(on Windows, </span><span class='windows'>a PowerShell session running under <a href='https://github.com/Microsoft/Terminal'>Windows Terminal</a></span><span class='no-js'>)</span> and follow along.
</p>
<aside>
<h3>Tip</h3>
<p>
The following command pipes the installation script into your shell. <strong>Make sure you understand what any script does before piping it into your shell.</strong>
</p>
<p>
This installation script downloads the correct Site.js binary for your platform and places it on your path. View the source of the <a class='linux macos' href='https://should-i-pipe.it/https://sitejs.org/install'>Linux and macOS</a><span class='no-js preserve-whitespace'> and </span><a class='windows' href='https://should-i-pipe.it/https://sitejs.org/install.txt'>Windows</a> script<span class='no-js'>s</span>.
</p>
<p id='copy-tip' class='hidden'>
You can <span class='just-click'>click</span><span class='click-and-tap'>/</span><span class='just-tap'>tap</span> the code samples to copy them to the clipboard.
</p>
</aside>
<h4 class='no-js'>Linux</h4>
<pre class='linux'><code class='language-bash'>wget -qO- https://sitejs.org/install | bash</code></pre>
<h4 class='no-js'>macOS</h4>
<pre class='macos'><code class='language-bash'>curl -s https://sitejs.org/install | bash</code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-powershell'>iex(iwr -UseBasicParsing https://sitejs.org/install.txt).Content</code></pre>
</li>
<li>
<h3>Create a basic static web page.</h3>
<ol>
<li>
<p>
Create a folder.
</p>
<pre><code class='language-bash'>mkdir hello-world</code></pre>
</li>
<li>
<p>
Enter the folder.
</p>
<pre><code class='language-bash'>cd hello-world</code></pre>
</li>
<li>
<p>
Write the string <code>'Hello, world'</code> into a file called <em>index.html</em>, creating or overwriting it as necessary.
</p>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-bash'>echo 'Hello, world' > index.html</code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-powershell'>echo 'Hello, world' | Out-File -Encoding UTF8 index.html</code></pre>
</li>
</ol>
</li>
<li>
<h3>Start the server.</h3>
<pre><code class='language-bash'>site</code></pre>
<p>
Site.js starts serving your hello-world site at <a href='https://localhost'>https://localhost</a>. Go there in your browser to see your “Hello, world!” message.
</p>
<p>
<strong>Congratulations, you’re up and running with Site.js!</strong>
</p>
<aside>
<h3>Tip</h3>
<p>
<strong>Do the same thing on a VPS server</strong> and start the server using <code>site enable</code> and you’ll have a secure production web daemon that survives errors/reboots and auto-updates itself.
</p>
</aside>
</li>
</ol>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Static sites -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='static-sites'>
<h2>Static sites with Hugo</h2>
<div class='introduction'>
<p>
The type of site you just created is an old-school static site. You can create static sites with Site.js using plain old HTML, CSS, and JavaScript. Site.js will automatically serve any files it finds in the folder you start it on as static files.
</p>
<p>
For larger sites, hand-rolling your static site might become cumbersome. That’s why Site.js comes bundled with the <a href='https://gohugo.io'>Hugo static site generator</a>.
</p>
<p>
Let’s create a new Hugo site and start serving it.
</p>
</div>
<ol>
<li>
<h3>Set up a new project.</h3>
<ol>
<li>
<p>
Create a folder.
</p>
<pre><code class='language-bash'>mkdir my-hugo-site</code></pre>
</li>
<li>
<p>
Enter the folder.
</p>
<pre><code class='language-bash'>cd my-hugo-site</code></pre>
</li>
</ol>
</li>
<li>
<h3>Create a new Hugo site.</h3>
<pre><code class='language-bash'>site hugo new site .hugo</code></pre>
<aside>
<h3>Tip</h3>
<p>
You can pass any command you would normally pass to Hugo using Site.js’s integrated Hugo instance: <code>site hugo <any valid Hugo command></code>. So the above command is equivalent to running <code>hugo new site .hugo</code> if you have Hugo installed separately.)
</p>
</aside>
</li>
<li>
<h3>Create a basic page layout.</h3>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'><body><h1>Hello, world!</h1></body></span><span class='hljs-string'>'</span><span class='language-bash'> > .hugo/layouts/index.html</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-powershell'>echo '</span><span class='language-html'><body><h1>Hello, world!</h1></body></span><span class='hljs-string'>'</span><span class='language-powershell'> | Out-File -Encoding UTF8 .hugo/layouts/index.html</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
For Site.js’s live reload to work, your web page must contain at least a <code><body></code> tag.
</p>
</aside>
</li>
<li>
<h3>Start the server.</h3>
<pre><code class='language-bash'>site</code></pre>
<p>
Site.js starts serving your Hugo site at <a href='https://localhost'>https://localhost</a>. Go there in your browser to see your “Hello, world!” message.
</p>
<p>
<strong>Congratulations, you just created your first Hugo site with Site.js!</strong>
</p>
<aside>
<h3>Tip</h3>
<p>
If Site.js finds a folder called <em>.hugo</em> in your site’s root, it will build it using its integrated Hugo instance (you don’t need to install Hugo separately) and place the generated files into a folder called <em>.generated</em> in your site’s root. It will also automatically serve these files.
</p>
<p>
If you alter the file you created in this example, Site.js will automatically rebuild it using Hugo and your browser will reflect the changes immediately using live reload.
</p>
<p>
See the <a href='https://github.com/small-tech/site.js#static-site-generation'>Site.js Hugo documentation</a> and <a href='https://gohugo.io/documentation/'>the official Hugo documentation</a> for detailed information on how Hugo works and how it’s integrated into Site.js.
</p>
</aside>
</li>
</ol>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Dynamic sites -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='dynamic-sites'>
<h2 id='dynamic'>Dynamic sites with DotJS</h2>
<div class='introduction'>
<p>
Site.js does not limit you to creating and serving fully static sites. You can easily add dynamic functionality to your static sites or create fully dynamic sites.
</p>
<p>
The easiest way to get started with dynamic sites is to use DotJS. DotJS gives you PHP-like simplicity in JavaScript using simple <code>.js</code> files.
</p>
<p>
Follow along to create a very basic dynamic site that updates a counter every time the home page is reloaded.
</p>
</div>
<ol>
<li>
<h3>Create the project structure.</h3>
<ol>
<li>
<p>
Create the folders.
</p>
<p>
Make a project folder called <em>count</em> and a special nested folder in that called <em>.dynamic</em>:
</p>
<pre><code class='language-bash'>mkdir -p count/.dynamic</code></pre>
</li>
<li>
<p>
Enter the dynamic routes folder.
</p>
<pre><code class='language-bash'>cd count/.dynamic</code></pre>
</li>
</ol>
<aside>
<h3>Tip</h3>
<p>
<strong>.dynamic</strong> is a special folder in that Site.js knows to look for it in your project. If it finds it, it knows to run its contents as a dynamic route instead of serving it as static content.
</p>
</aside>
</li>
<li>
<h3>Create a dynamic route.</h3>
<p>
Make a file called index.js that holds the dynamic counter route:
</p>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-js'>i=0; module.exports=(_, res) => res.html(`${++i}`)</span><span class='hljs-string'>'</span><span class='language-bash'> > index.js</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-powershell'>echo '</span><span class='language-js'>i=0; module.exports=(_, res) => res.html(`${++i}`)</span><span class='hljs-string'>'</span><span class='language-powershell'> | Out-File -Encoding UTF8 index.js</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
If the code here looks cryptic to you, <a href='#dynamic-route-longer-example'>see the longer version</a>.
</p>
</aside>
</li>
<li>
<h3>Serve your site.</h3>
<pre><code class='language-bash'>site ..</code></pre>
<aside>
<h3>Tip</h3>
<p>
You don’t have to be in the folder you want Site.js to serve, as in the previous examples. As shown here, you can specify the folder as an argument to the <code>site</code> command. In this case, we want to serve the whole site from the parent folder, not just the <code>.dynamic</code> folder.)
</p>
</aside>
<p>
Hit <a href='https://localhost'>https://localhost</a> and refresh to see the counter update.
</p>
<p>
<strong>Congratulations, you just made your first fully dynamic DotJS site!</strong>
</p>
<aside id='dynamic-route-longer-example' class='tip'>
<h3>Tip</h3>
<p>
If you found the code in the dynamic route we created hard to read, you’re not alone. It was purposefully written to keep the example short enough to enter into terminal.
</p>
<p>
Here’s a slightly more verbose version of <code>index.js</code> that’s easier to understand that you might want to enter using your favourite code editor:
</p>
<pre><code class='language-js'>let counter = 0
module.exports = (request, response) => {
response.html(`<h1>Hit count: ${++counter}</h1>`)
}</code></pre>
<p>
If this reminds you of a route in <a href='https://expressjs.com'>Express</a>, that’s because that’s exactly what it is.
</p>
<p>
The only difference is that you don’t have to write any other code or worry about anything else including installing Node.js, provisioning TLS certificates, ensuring your site automatically restarts on reboots, etc.
</p>
<p>
(You might not recognise the <code>.html()</code> method on the <code>response</code> object as that’s a Site.js addition to make your life easier. It corresponds to writing <code>.type('html').end()</code> in Express.)
</p>
<p>
You can also define <a href='https://github.com/small-tech/site.js/blob/master/README.md#specifying-parameters'>named parameters</a> for your DotJS routes and Site.js does not limit you to using DotJS, either.
</p>
<p>
If you want to, you can use the full power of Express and Node.js with <a href='https://github.com/small-tech/site.js#advanced-routing-routesjs-file'>advanced routing</a>. And, since it is just Node.js under the hood, you can do anything you can with Node.js including <a href='https://github.com/small-tech/site.js#using-node-modules'>using node modules</a>, etc.
</p>
<p>
See the Site.js documentation for <a href='https://github.com/small-tech/site.js#dynamic-sites'>more information on building dynamic sites</a>.
</p>
</aside>
</li>
</ol>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- WebSockets -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='websockets'>
<h2>WebSockets</h2>
<div class='introduction'>
<p>
In addition to static routes and dynamic HTTPS routes, you can also specify secure WebSocket (WSS) routes in DotJS. And you can mix all three types of routes as you please.
</p>
<p>
To see WebSockets in action, let’s create a very simple chat app.
</p>
</div>
<ol>
<li>
<h3>Create the project structure.</h3>
<pre><code class='language-bash'>mkdir -p chat/.dynamic/.wss</code></pre>
</li>
<li>
<h3>Create the chat server.</h3>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-js'>
module.exports = function (client, request) {
// Set the room based on the route’s URL.
client.room = this.setRoom(request)
// Create a message handler.
client.on("message", message => {
// Broadcast a received message to everyone in the room.
this.broadcast(client, message)
})
}</span>
<span class='hljs-string'>'</span><span class='language-bash'> > chat/.dynamic/.wss/chat.js</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-powershell'>echo '</span><span class='language-js'>
module.exports = function (client, request) {
// Set the room based on the route URL.
client.room = this.setRoom(request)
// Create a message handler.
client.on("message", message => {
// Broadcast a received message to everyone in the room.
this.broadcast(client, message)
})
}</span>
<span class='hljs-string'>'</span> <span class='language-powershell'>| Out-File -Encoding UTF8 chat/.dynamic/.wss/chat.js</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
Note that we did not use an arrow function to define the route this time. This is because we need access to higher-level built-in functionality on the route like setting the room and broadcasting a message to everyone in the room that we access via the <code>this</code> reference.
</p>
</aside>
</li>
<li>
<h3>Create the chat client.</h3>
<h4 class='no-js'>Linux and macOS</h4>
<pre class='linux macos'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'>
<!doctype html>
<html lang="en">
<title>Chat</title>
<h1>Chat</h1>
<form name="messageForm">
<input name="message" type="text">
<button>Send</button>
</form>
<h2>Messages</h2>
<ul id="messageList"></ul>
<script></span><span class='language-js'>
const socket = new WebSocket(`wss://${window.location.hostname}/chat`)
const showMessage = message => {
const list = document.querySelector("#messageList")
list.innerHTML += `<li>${message.text}</li>`
}
socket.onmessage = message => {
showMessage(JSON.parse(message.data))
}
document.messageForm.addEventListener("submit", event => {
const message ={ text: event.target.message.value }
socket.send(JSON.stringify(message))
showMessage(message)
event.preventDefault()
})</span><span class='language-html'>
</script>
</span><span class='hljs-string'>'</span><span class='language-bash'> > chat/index.html</span></code></pre>
<h4 class='no-js'>Windows</h4>
<pre class='windows'><code class='language-mixed'><span class='language-bash'>echo '</span><span class='language-html'>
<!doctype html>
<html lang="en">
<title>Chat</title>
<h1>Chat</h1>
<form name="messageForm">
<input name="message" type="text">
<button>Send</button>
</form>
<h2>Messages</h2>
<ul id="messageList"></ul>
<script></span><span class='language-js'>
const socket = new WebSocket(`wss://${window.location.hostname}/chat`)
const showMessage = message => {
const list = document.querySelector("#messageList")
list.innerHTML += `<li>${message.text}</li>`
}
socket.onmessage = message => {
showMessage(JSON.parse(message.data))
}
document.messageForm.addEventListener("submit", event => {
const message ={ text: event.target.message.value }
socket.send(JSON.stringify(message))
showMessage(message)
event.preventDefault()
})</span><span class='language-html'>
</script>
</span><span class='hljs-string'>'</span><span class='language-powershell'>| Out-File -Encoding UTF8 chat/index.html</span></code></pre>
<aside>
<h3>Tip</h3>
<p>
You can mix static and dynamic routes in your projects. Anything outside of the <code>.dynamic</code> folder – like our chat client here – will be served as a static page.
</p>
</aside>
</li>
<li>
<h3>Launch the server.</h3>
<pre><code class='language-bash'>site chat</code></pre>
<p>
Start the server.
</p>
<p>
To test your chat app, open up two or more web browser windows at <a href='https://localhost'>https://localhost</a> and play with the chat interface.
</p>
<aside>
<h3>Tip</h3>
<p>
The broadcast method, by default, has rudimentary client filtering and only sends a message to clients other than the one that originally sent the message and only to clients connected to the same route (or “room”).
</p>
<p>
For a fully-documented version of the above example, see <a href='https://github.com/small-tech/site.js/tree/master/examples/simple-chat'>the source code for the Simple Chat example.</a>
</p>
<p>
To run that example, first clone the Site.js source code repository:
</p>
<pre><code class='language-bash'>git clone https://github.com/small-tech/site.js</code></pre>
<p>
Then run the simple chat example using Site.js:
</p>
<pre><code class='language-bash'>site site.js/examples/simple-chat</code></pre>
<p>
Finally, open <a href='https://localhost'>https://localhost</a> in multiple browser windows and play with the chat interface.
</p>
<p>
The WebSocket functionality is from <a href='https://github.com/aral/express-ws'>our fork of express-ws</a> (which in turn uses <a href='https://github.com/websockets/ws'>ws</a>). Both of those links have more usage details.
</p>
<p>
For full details, see the <a href='https://github.com/small-tech/site.js/blob/master/README.md#dynamic-sites'>Dynamic Sites documentation</a> and view the <a href='https://github.com/small-tech/site.js/tree/master/examples'>examples</a>.
</p>
</aside>
</li>
</ol>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Database -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='database'>
<h2>Database</h2>
<div class='introduction'>
<p>Site.js also has a fast and simple JavaScript Database (JSDB) built into it. You can refer to the database for your app any of your routes using the global <code>db</code> instance.</p>
<p>Let’s see how easy it is to use JSDB by persisting the messages our simple chat app.</p>
</div>
<ol>
<li>
<h3>Update the server.</h3>
<p>The code you need to add is presented <strong>in boldface.</strong></p>
<pre><code class='language-mixed'><span class='language-javascript'>module.exports = function (client, request) {</span>
<span class='language-javascript strong'>// Ensure the messages table exists.
if (!db.messages) {
db.messages = []
}</span><span class='language-javascript'>
// Set the room based on the route’s URL.
client.room = this.setRoom(request)
</span><span class='language-javascript strong'>// Send new clients all existing messages.
client.send(JSON.stringify(db.messages))</span><span class='language-javascript'>
// Create a message handler.
client.on('message', message => {
</span><span class='language-javascript strong'>// Parse the message JSON string into a JavaScript object.
const parsedMessage = JSON.parse(message)
// Persist the message.
db.messages.push(parsedMessage)</span><span class='language-javascript'>
// Broadcast a received message to everyone in the room.
this.broadcast(client, message)
})
}</span></code></pre>
</li>
<li>
<h3>Update the client.</h3>
<p>You need to update the client so that it can handle the initial list of messages that is sent when someone joins the chat.</p>
<p>Again, the code you need to add is presented <strong>in boldface.</strong></p>
<pre><code class='language-mixed'><span class='language-html'><!doctype html>
<html lang="en">
<title>Chat</title>
<h1>Chat</h1>
<form name="messageForm">
<input name="message" type="text">
<button>Send</button>
</form>
<h2>Messages</h2>
<ul id="messageList"></ul>
<script></span><span class='language-js'>
const socket = new WebSocket(`wss://${window.location.hostname}/chat`)
const showMessage = message => {
const list = document.querySelector("#messageList")
list.innerHTML += `<li>${message.text}</li>`
}
</span><span class='language-js strong'>socket.onmessage = websocketMessage => {
// Deserialise the data.
const data = JSON.parse(websocketMessage.data)
if (Array.isArray(data)) {
// Display initial list of messages we get when we join a room.
data.forEach(message => showMessage(message))
} else {
// Display a single message.
showMessage(data)
}
}</span><span class='language-js'>
document.messageForm.addEventListener("submit", event => {
const message = { text: event.target.message.value }
socket.send(JSON.stringify(message))
showMessage(message)
event.preventDefault()
})</span><span class='language-html'>
</script></code></pre>
</li>
</ol>
<aside>
<h3>Tip</h3>
<p>This barely scratches the surface of what JSDB is and what you can do with it.</p>
<p>JSDB is an in-memory JavaScript database that persists tables to append-only transaction logs in <a href='https://github.com/small-tech/jsdb/blob/master/README.md#javascript-data-format-jsdf'>JavaScript Data Format (JSDF)</a> which is a subset of JavaScript.</p>
<p>It also has its own query language called <a href='https://github.com/small-tech/jsdb/blob/master/README.md#javascript-query-language-jsql'>JavaScript Query Language (JSQL)</a>. So, for example, if you wanted to get a list of messages that contained the hashtag <code>#BLM</code>, you could do so like this:</p>
<pre><code class='language-javascript'>db.messages.where('text').includesCaseInsensitive('#blm').get()</code></pre>
<p>Find out more about JSDB in this <a href="https://ar.al/2020/10/20/introducing-jsdb/">introductory blog post</a>, in <a href="https://source.small-tech.org/site.js/app#persisting-data-on-the-server-with-javascript-database-jsdb">the Site.js JSDB documentation</a>, and in <a href="https://github.com/small-tech/jsdb/blob/master/README.md">the JSDB documentation</a>. See <a href="https://github.com/small-tech/site.js/tree/master/examples/persisted-chat">the source code for the persisted chat example</a> for a more comprehensive version of the sample presented here.</p>
</p>
</aside>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Proxy Servers -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='proxy-servers'>
<h2>Proxy servers</h2>
<div class='introduction'>
<p>You can use Site.js as a proxy to add automatic TLS for HTTP and WebSocket to any web app.</p>
</div>
<ol>
<li>
<h3>Create a simple insecure service</h3>
<p>ForThe following is a simple HTTP server written in Python 3 (<em>server.py</em>) that runs insecurely on port 3000:</p>
<pre><code class='language-python'>from http.server import HTTPServer, BaseHTTPRequestHandler
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, from Python!')
server = HTTPServer(('localhost', 3000), MyRequestHandler)
server.serve_forever()
</code></pre>
</li>
<li>
<h3>Run it</h3>
<p>To keep things simple, the following command will simply run it in the background.</p>
<pre><code class='language-bash'>$ python3 server &</code></pre>
</li>
<li>
<h3>Serve it securely using Site.js</h3>
<pre><code class='language-bash'>$ site enable :3000</code></pre>
</li>
</ol>
<aside>
<h3>Tip</h3>
<p>Of course, in a real-world scenario, you would also install and run the python app as a systemd service so it too survives restarts, etc., but that is beyond the scope of Site.js.</p>
<p>(To stop the python server, run <code>fg</code> to bring it into the foreground and then press <em>Ctrl+C</em> to exit the process.)</p>
</aside>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Further reading -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='further-reading' class='highlights'>
<h2>Further reading</h2>
<ul>
<li>
<h3>Multi-device testing</h3>
<p>
<a href='https://github.com/small-tech/site.js#testing-servers-hostname'>Start a server with <code>site @hostname</code></a> and use a service like <a href='https://github.com/small-tech/site.js#making-your-server-public'>ngrok</a> to test on all your devices with live reload or to test your site with others over the Internet.
</p>
</li>
<li>
<h3>Sync</h3>
<p>
Deploy using <a href='https://github.com/small-tech/site.js#sync'>the built-in sync feature</a>. Live blog using <a href='https://github.com/small-tech/site.js#live-sync'>Live Sync</a>.
</p>
</li>
<li>
<h3>Evergreen Web</h3>
<p>
<a href='https://github.com/small-tech/site.js#native-support-for-an-evergreen-web'>Migrate your existing sites</a> without breaking links on the web with native support for <a href='https://github.com/small-tech/site.js#the-archival-cascade'>archival cascades</a> and <a href='https://github.com/small-tech/site.js#native-404--302-support'>native 404 → 302 support</a>.
</p>
</li>
<li>
<h3>Custom error pages</h3>
<p>
Easily create <a href='https://github.com/small-tech/site.js#custom-error-pages'>custom 404 and 500 error pages</a>.
</p>
</li>
<li>
<h3>Ephemeral statistics</h3>
<p>
See your most popular pages and discover broken links using privacy-respecting, <a href='https://github.com/small-tech/site.js#ephemeral-statistics'>ephemeral statistics</a> that are reset on every server start.
</p>
</li>
<li>
<h3>Documentation</h3>
<p>
Learn more about building static and dynamic web sites and applications using Site.js in the <a href='https://github.com/small-tech/site.js#sitejs'>Site.js documentation</a>.
</p>
</li>
</ul>
</section>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- About -->
<!-- -->
<!------------------------------------------------------------------------------------>
<section id='about'>
<h2>Like this? Fund us!</h2>
<p>
Made with love by <a href='https://small-tech.org'>Small Technology Foundation</a>.
</p>
<p>
We are a tiny not-for-profit based in Ireland that makes tools for people like you – not for startups, enterprises, or governments. And we’re also funded by people like you. <strong>If you like our work and want to help us continue to exist, please <a href='https://small-tech.org/fund-us/'>fund us</a>.</strong>
</p>
<p>
Site.js is <a href='https://small-tech.org/about#small-technology'>Small Technology</a>.
</p>
</section>
</main>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Footer -->
<!-- -->
<!------------------------------------------------------------------------------------>
<footer>
<img src='/images/illustrations/site.js-logo.svg' id='logo' alt='Site.js logo: a sprouting sapling with a short brown stem and two green leaves'>
<p id='copyright'>
<small>
Copyright ⓒ 2019-present, <a href='https://small-tech.org'>Small Technology Foundation</a>. All content on this site is released under <a href='https://creativecommons.org/licenses/by-sa/4.0/'>Creative Commons Attribution-ShareAlike</a>. Site.js is free software licensed under <a href='https://www.gnu.org/licenses/agpl-3.0.en.html'>AGPLv3 or later</a>. The illustration in the header is modified from the work of Katerina Limpitsouni on <a href='https://undraw.co/'>unDraw</a>. The Site.js sprout logo, Site.js, and Small Technology Foundation are trademarks of Small Technology Foundation.
</small>
</p>
</footer>
<!------------------------------------------------------------------------------------>
<!-- -->
<!-- Scripts -->
<!-- -->
<!------------------------------------------------------------------------------------>
<!-- Start: scripts -->
<script src='js/index.js'></script>
<!-- End: scripts -->
</body>
</html>