@tobiasgjerstrup/microhttp-server 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2026 Tobias Gjerstrup
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # MicroHTTP Server
2
+
3
+ A small, lightweight, zero-dependency wrapper around Node.js' built-in `http` module for quickly defining route handlers.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @tobiasgjerstrup/microhttp-server
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import createApp from "@tobiasgjerstrup/microhttp-server";
15
+
16
+ const app = createApp();
17
+
18
+ app.get("/", (req, res) => {
19
+ res.writeHead(200, { "Content-Type": "text/plain" });
20
+ res.end("Hello, world!");
21
+ });
22
+
23
+ app.listen(3000, "127.0.0.1", () => {
24
+ console.log("Listening on http://127.0.0.1:3000");
25
+ });
26
+ ```
27
+
28
+ ## API
29
+
30
+ ### `createApp()`
31
+
32
+ Returns an app instance with the following methods:
33
+
34
+ #### `app.get(path, handler)`
35
+ #### `app.post(path, handler)`
36
+ #### `app.put(path, handler)`
37
+ #### `app.delete(path, handler)`
38
+
39
+ Register a route handler for the given HTTP method and path.
40
+
41
+ - `path` — exact URL path to match (e.g. `"/users"`)
42
+ - `handler(req, res)` — standard Node.js `http.IncomingMessage` / `http.ServerResponse` handler
43
+
44
+ #### `app.listen(port, callback)`
45
+ #### `app.listen(port, hostname, callback)`
46
+
47
+ Start the HTTP server on the given port and optional hostname.
48
+
49
+ Unmatched routes respond with `404 Not Found`.
50
+
51
+ ## License
52
+
53
+ ISC
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ console.log("Hello world!");
2
+ export {};
package/dist/main.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import http from 'node:http';
2
+ declare function createApp(): {
3
+ get: (path: string, handler: (req: http.IncomingMessage, res: http.ServerResponse) => void) => void;
4
+ post: (path: string, handler: (req: http.IncomingMessage, res: http.ServerResponse) => void) => void;
5
+ put: (path: string, handler: (req: http.IncomingMessage, res: http.ServerResponse) => void) => void;
6
+ delete: (path: string, handler: (req: http.IncomingMessage, res: http.ServerResponse) => void) => void;
7
+ listen: (port: number, hostnameOrCallback: string | (() => void), callback?: () => void) => void;
8
+ };
9
+ export default createApp;
10
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,iBAAS,SAAS;gBA+BF,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,cAAc,KAAK,IAAI;iBAG7E,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,cAAc,KAAK,IAAI;gBAG/E,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,cAAc,KAAK,IAAI;mBAG3E,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,GAAG,EAAE,IAAI,CAAC,cAAc,KAAK,IAAI;mBA7BzE,MAAM,sBAAsB,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,aAAa,MAAM,IAAI;EAkC/F;AAED,eAAe,SAAS,CAAC"}
package/dist/main.js ADDED
@@ -0,0 +1,44 @@
1
+ import http from 'node:http';
2
+ function createApp() {
3
+ const routes = [];
4
+ function addRoute(method, path, handler) {
5
+ routes.push({ method, path, handler });
6
+ }
7
+ function matchRoute(method, url) {
8
+ return routes.find(r => r.method === method && r.path === url);
9
+ }
10
+ function listen(port, hostnameOrCallback, callback) {
11
+ const server = http.createServer((req, res) => {
12
+ const route = matchRoute(req.method, req.url ?? '');
13
+ if (route) {
14
+ route.handler(req, res);
15
+ }
16
+ else {
17
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
18
+ res.end('Not found');
19
+ }
20
+ });
21
+ if (typeof hostnameOrCallback === 'function') {
22
+ server.listen(port, hostnameOrCallback);
23
+ }
24
+ else {
25
+ server.listen(port, hostnameOrCallback, callback);
26
+ }
27
+ }
28
+ return {
29
+ get: (path, handler) => {
30
+ addRoute('GET', path, handler);
31
+ },
32
+ post: (path, handler) => {
33
+ addRoute('POST', path, handler);
34
+ },
35
+ put: (path, handler) => {
36
+ addRoute('PUT', path, handler);
37
+ },
38
+ delete: (path, handler) => {
39
+ addRoute('DELETE', path, handler);
40
+ },
41
+ listen
42
+ };
43
+ }
44
+ export default createApp;
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@tobiasgjerstrup/microhttp-server",
3
+ "version": "1.0.0",
4
+ "description": "A small wrapper for nodejs' http server",
5
+ "homepage": "https://github.com/tobiasgjerstrup/npm-MicroHTTP-Server#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/tobiasgjerstrup/npm-MicroHTTP-Server/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/tobiasgjerstrup/npm-MicroHTTP-Server.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "Tobias Gjerstrup",
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "package.json",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "scripts": {
34
+ "start": "cd playground && npm start",
35
+ "lint": "eslint . --max-warnings=0",
36
+ "test": "vitest",
37
+ "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
38
+ "prepublishOnly": "npm run build && npm run lint && npm run test"
39
+ },
40
+ "devDependencies": {
41
+ "@eslint/js": "^10.0.1",
42
+ "@types/node": "^25.5.0",
43
+ "eslint": "^10.1.0",
44
+ "globals": "^17.4.0",
45
+ "jiti": "^2.6.1",
46
+ "typescript": "~6.0.0",
47
+ "typescript-eslint": "^8.58.0",
48
+ "vitest": "^4.1.1"
49
+ }
50
+ }