fnode_http
v0.0.4
Published
Experimental HTTP 1.1 Library
Downloads
7
Readme
📦 fnode_http
An experimental Node.js HTTP 1.1 library
⚡ Features
- Extremely lightweight HTTP server
- Compatible with CommonJS (Node.js)
- Supports both JavaScript and TypeScript
📁 Installation
npm install fnode_http🚀 JavaScript Example
const fnode = require("fnode_http");
function home() {
return "hello";
}
function data() {
return JSON.stringify([
{ id: 1, ok: true },
{ id: 2, ok: false },
]);
}
function not_found() {
return "not found";
}
const routes = new Map([
["/", home],
["/data", data],
]);
fnode.router((path) => {
const fn = routes.get(path);
return fn ? fn() : not_found();
});
fnode.server("0.0.0.0:8080");▶️ Start the Server
node your_main_file.js🟦 TypeScript Setup & Example
1. Install TypeScript and Node type definitions
npm install --save-dev typescript @types/node2. TypeScript Code Example (ts.ts)
const fnode = require("fnode_http");
function home(): string {
return "hello";
}
function data(): string {
return JSON.stringify([
{ id: 1, ok: true },
{ id: 2, ok: false },
]);
}
function not_found(): string {
return "not found";
}
const routes = new Map<string, () => string>([
["/", home],
["/data", data],
]);
fnode.router((path: string): string => {
const fn = routes.get(path);
return fn ? fn() : not_found();
});
fnode.server("0.0.0.0:8080");3. Compile the TypeScript file
tsc your_main_file.ts --target es2020 --lib es2020 --module commonjsThis will create
your_main_file.js
4. Run
node your_main_file.js📜 License
MIT
