Skip to content

Commit 1894cfb

Browse files
committed
feat: implement basic HTTP server with customizable options and add chalk dependency for console output styling
1 parent 106bdfc commit 1894cfb

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

eslint.config.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import dhzh from '@dhzh/eslint-config';
22

3-
export default dhzh({});
3+
export default dhzh({
4+
rules: {
5+
'no-console': 'off',
6+
},
7+
});

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
},
2222
"main": "./dist/index.js",
2323
"types": "./dist/index.d.ts",
24-
"files": ["dist"],
24+
"files": [
25+
"dist"
26+
],
2527
"scripts": {
2628
"build": "tsup",
2729
"build:dev": "tsup --watch",
2830
"dev": "tsx watch --clear-screen=false src/index.ts",
2931
"release": "bumpp",
3032
"release:publish": "pnpm release && pnpm publish"
3133
},
34+
"dependencies": {
35+
"chalk": "^5.3.0"
36+
},
3237
"devDependencies": {
3338
"@dhzh/eslint-config": "^0.15.1",
3439
"@types/node": "^22.10.1",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1-
/* eslint-disable no-console */
2-
console.log('test');
3-
console.log('test');
1+
import process from 'node:process';
2+
import * as http from 'node:http';
3+
import * as os from 'node:os';
4+
import * as fs from 'node:fs/promises';
5+
import * as path from 'node:path';
6+
import chalk from 'chalk';
7+
import type { ServerOptions } from './types';
8+
9+
class Server {
10+
port: number = 8080;
11+
baseDir: string = process.cwd();
12+
13+
constructor(options?: ServerOptions) {
14+
if (options?.port) {
15+
this.port = options.port;
16+
}
17+
if (options?.baseDir) {
18+
this.baseDir = options.baseDir;
19+
}
20+
}
21+
22+
start() {
23+
const server = http.createServer(async (req, res) => {
24+
try {
25+
const wholePath = path.join(this.baseDir, req.url ?? '/');
26+
const stat = await fs.stat(wholePath);
27+
if (stat.isDirectory()) {
28+
const content = await fs.readdir(wholePath);
29+
res.end(content.join('\n'));
30+
} else {
31+
res.end('Not a directory');
32+
}
33+
} catch {
34+
res.end('Not found');
35+
}
36+
});
37+
server.listen(this.port, () => {
38+
console.log(chalk.yellow('Server is running on:'));
39+
this
40+
.getOsHosts()
41+
.forEach((host) => console.log(`\x20\x20${host}`));
42+
});
43+
}
44+
45+
private getOsHosts() {
46+
return Object
47+
.values(os.networkInterfaces())
48+
.flat()
49+
.filter((item) => item?.family === 'IPv4')
50+
.map((item) => `http://${item.address}:${chalk.cyan(this.port)}`);
51+
}
52+
}
53+
54+
const server = new Server();
55+
server.start();

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ServerOptions {
2+
port?: number;
3+
baseDir?: string;
4+
}

0 commit comments

Comments
 (0)