1 | [![Version](https://img.shields.io/npm/v/httpdir.svg)](https://github.com/johansatge/httpdir/releases)
|
2 | [![Downloads](https://img.shields.io/npm/dm/httpdir.svg)](https://www.pkgstats.com/pkg:httpdir)
|
3 | [![Last commit](https://badgen.net/github/last-commit/johansatge/httpdir)](https://github.com/johansatge/httpdir/commits/master)
|
4 | [![Test](https://github.com/johansatge/httpdir/actions/workflows/test.yml/badge.svg)](https://github.com/johansatge/httpdir/actions)
|
5 | [![Codecov](https://codecov.io/gh/johansatge/httpdir/branch/master/graph/badge.svg?token=IGLWMA4BUI)](https://codecov.io/gh/johansatge/httpdir)
|
6 | [![Install Size](https://badgen.net/packagephobia/install/httpdir)](https://packagephobia.com/result?p=httpdir)
|
7 |
|
8 | ![Icon](icon.png)
|
9 |
|
10 | > Simple, zero dependency command-line HTTP server for static local files
|
11 |
|
12 | <img src="screenshot.png" alt="Safari window with a list of files">
|
13 |
|
14 | ---
|
15 |
|
16 | * [Installation](#installation)
|
17 | * [Usage](#usage)
|
18 | * [CLI](#cli)
|
19 | * [Node module](#node-module)
|
20 | * [Changelog](#changelog)
|
21 | * [License](#license)
|
22 |
|
23 | ## Installation
|
24 |
|
25 | _This module needs Node `>=14`._
|
26 |
|
27 | Install with [npm](https://www.npmjs.com/):
|
28 |
|
29 | ```bash
|
30 | $ npm install httpdir --global
|
31 | ```
|
32 |
|
33 | ## Usage
|
34 |
|
35 | ### CLI
|
36 |
|
37 | ```bash
|
38 | httpdir <path> <port>
|
39 | ```
|
40 |
|
41 | Example:
|
42 |
|
43 | ```bash
|
44 | # This will start a local server on port `8090`,
|
45 | # with `~/Desktop` as root directory
|
46 | httpdir ~/Desktop 8090
|
47 | ```
|
48 |
|
49 | Default path is `.`, default port is `8080`.
|
50 |
|
51 | ### Node module
|
52 |
|
53 | Import `httpdir` and create a server:
|
54 | ```js
|
55 | const httpdir = require('httpdir')
|
56 | const server = httpdir.createServer({
|
57 | basePath: '/some/path', // Optional, default is "."
|
58 | httpPort: 9898, // Optional, default is 8080
|
59 | })
|
60 | ```
|
61 | Main events can be listened to:
|
62 | ```js
|
63 | server.onStart((settings) => {
|
64 | console.log('Server started')
|
65 | console.log('Base path is:', settings.basePath)
|
66 | console.log('Port is:', settings.httpPort)
|
67 | console.log('Available URLs:', settings.urls.join(', '))
|
68 | })
|
69 | ```
|
70 | ```js
|
71 | server.onStop(() => {
|
72 | console.log('Server stopped')
|
73 | })
|
74 | ```
|
75 | ```js
|
76 | server.onResponse((response) => {
|
77 | console.log('Requested path was:', response.requestedPath)
|
78 | console.log('Requested method was:', response.requestedMethod)
|
79 | console.log('Response code is:', response.httpCode)
|
80 | })
|
81 | ```
|
82 | ```js
|
83 | server.onError((error) => {
|
84 | console.log('Server error', error)
|
85 | })
|
86 | ```
|
87 | After event listeners have been attached if needed, start the server:
|
88 | ```js
|
89 | server.start()
|
90 | ```
|
91 | And stop:
|
92 | ```js
|
93 | server.stop()
|
94 | ```
|
95 |
|
96 | ## Changelog
|
97 |
|
98 | This project uses [semver](http://semver.org/).
|
99 |
|
100 | | Version | Date | Notes |
|
101 | | --- | --- | --- |
|
102 | | `2.1.0` | 2023-1-07 | Support [`Range`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range) request header |
|
103 | | `2.0.1` | 2023-02-12 | Fix `onResponse` event for favicons |
|
104 | | `2.0.0` | 2023-02-04 | Expose node module<br>Drop Node 12 support |
|
105 | | `1.4.0` | 2023-01-29 | Improve startup info |
|
106 | | `1.3.0` | 2023-01-28 | Support more mime types |
|
107 | | `1.2.0` | 2022-01-10 | Update UI ([#1](https://github.com/johansatge/httpdir/pull/1)) |
|
108 | | `1.1.0` | 2021-09-25 | Stop relying on deprecated `url.parse()` |
|
109 | | `1.0.1` | 2021-07-04 | Fix execution issue on Unix |
|
110 | | `1.0.0` | 2021-07-04 | Initial version |
|
111 |
|
112 | ## License
|
113 |
|
114 | This project is released under the [MIT License](license.md).
|