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
|
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="aki">
<meta name="tags" content="software development, http, json, web development, static data, static files, rant">
<link rel="icon" type="image/png" href="cylo.png">
<link rel="stylesheet" href="style.css">
<title>You Can Serve Static Data Over HTTP</title>
<nav><p><a href="https://ignore.pl">ignore.pl</a></p></nav>
<article>
<h1>You Can Serve Static Data Over HTTP</h1>
<p class="subtitle">Published on 2022-01-27 20:00:00+01:00
<p>This will be short. I need to let some steam off and flush this thing out of my mind because it's already hanging
around for too long. The story goes like this: we're prototyping small tool for whatever and we're at the point where
there is a visible need to store some (most likely) static data. Oh, the thing is in JavaScript by the way.
<p>"Yeah, let's just put it into a JSON file and serve it over web server."
<p>Said me naively but turns out that there is a legitimate confusion on the other side of the trench.
<p>"Do you mean a database?"
<p>Let's end the flashback here, we already have enough background for what will follow. Now, hear me out, <em>your API
endpoint is just pretending to be a file.</em>
<p>You don't need a full blown web application back-end. You don't need an over-featured database engine that can
provide its own REST API endpoints with data. And you don't need a complex ORM library and couple your data model to it
and write your own endpoints. You don't need any of these and possibly others.
<p>You <em>may</em> need them, but never let your habits decide your designs.
<p>It's not illegal to use static files and it does work. Moreover, with a limited set of data it gives huge freedom to
developer to try out things and break them. It's suited for rapid prototyping but it also has its own place in
production environment (e.g., git).</p>
<img src="you_can_serve_static_data_over_http-1.png" alt="free junk">
<h2>Is it possible to learn this power?</h2>
<p>Not from a modern developer. You need to jump down the rabbit hole of the history of solid and proven solutions.
<p>First off, create some files. In my example we needed some static data for JavaScript, so we used JSON:
<pre>
[
{"type": 1, "name": "a"},
{"type": 1, "name": "b"}
]
</pre>
<p>Now, save it as a file called <code>things</code> without any extension at all. Something like this will do:
<pre>
$ pwd
/home/ignore/public
$ ls
things
</pre>
<p>It's ready to be served to the client. Start up your favourite server that can handle static data and position it at
the directory that you've selected:
<pre>
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
</pre>
<p>It can now be accessed at <code>http://localhost:8000/things</code>. If you are working with JS as I did that time,
shift the <code>public</code> content a bit to also serve the client application:
<pre>
$ ls . api
.:
index.html style.css main.js api/
api:
things
</pre>
<p>Dead simple, right? Now you know. Hey, you can even mock more complex APIs with a help of symlinks and creativity.
And if you worry about whether it works - just try it out. If it has some problems - force the server to set correct
mime type for files without extensions.
<p>Hopefully, next post will be more interesting, but this had to be done. Just had to. I may rest now.
<!--
Don't worry or go "ha ha, this guy got triggered by such thing" - you've been bamboozled. It's all have been an act.
Without it the whole post would be not only basic but also boring. One thing remains true: for some reason the idea of
this post was following me for quite a while now and I just wanted to get rid off it.
-->
</article>
<script src="https://stats.ignore.pl/track.js"></script>
|