Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add http.createStaticServer #45096

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
318610e
http: add `http.createStaticServer`
aduh95 Oct 20, 2022
77e0416
Add docs and parameters
aduh95 Oct 21, 2022
b1cb361
default to `'localhost'`, return server, update docs
aduh95 Oct 21, 2022
ac549c4
Apply suggestions from code review
aduh95 Oct 21, 2022
d3b189f
Ensure the server doesn't start if it's being required
aduh95 Oct 21, 2022
b6dbf4f
use options instead of function arguments
aduh95 Oct 21, 2022
bc6fd54
s/Simple/Static/
aduh95 Oct 21, 2022
39dbb48
add `mimeOverrides` option
aduh95 Oct 21, 2022
c53b51e
add `serveDotFiles` option
aduh95 Oct 21, 2022
c394473
add `filter` option
aduh95 Oct 22, 2022
6502cbc
`stream.promises.pipeline`
aduh95 Oct 23, 2022
9d408e7
add more validation
aduh95 Oct 23, 2022
9a9caa7
Revert "`stream.promises.pipeline`"
aduh95 Oct 23, 2022
bd9a6bd
fixup! add more validation
aduh95 Oct 23, 2022
a0c7864
pass two args to `filter`
aduh95 Oct 23, 2022
20e3d86
serve index files with correct MIME
aduh95 Oct 23, 2022
98a5563
add tests
aduh95 Oct 23, 2022
1259d43
add fixtures files
aduh95 Oct 23, 2022
f3d663d
lint
aduh95 Oct 23, 2022
60d3f0b
rename `http/server` -> `http/static`
aduh95 Oct 26, 2022
41fc901
list missing features in docs
aduh95 Oct 26, 2022
47d2f8b
add tests to ensure that using a "hidden" folder as root is blocked b…
aduh95 Nov 23, 2022
fa52ee0
Use 403 instead of 401
aduh95 Nov 23, 2022
968efde
fix type in docs
aduh95 Nov 23, 2022
5e018ef
remove `http/static` module
aduh95 Dec 7, 2022
3698f0c
add line return
aduh95 Dec 15, 2022
d8ce4b3
add `log` and `onStart` option to let user control logging
aduh95 Dec 19, 2022
0c91db4
add tests to ensure using `..` can't escape the root dir
aduh95 Dec 19, 2022
7b40455
add tests with encoded chars and several slashes in a row
aduh95 Dec 20, 2022
0bc9f5e
liint + `.json`
aduh95 Sep 18, 2023
29257f9
Apply suggestions from code review
aduh95 Sep 19, 2023
a5ea0d7
fix failing test
aduh95 Apr 3, 2024
c1e6869
move `mime` out of `requestHandler`
aduh95 Apr 3, 2024
2e63aba
fix lint
aduh95 Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
aduh95 and VoltrexKeyva committed Apr 3, 2024
commit ac549c49b37cdff32c5594563846a3a7674d2ab9
11 changes: 6 additions & 5 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3573,20 +3573,20 @@ added: REPLACEME

> Stability: 0 - Experimental

* `directory` {string|URL} Root directory from which files would be serve.
* `directory` {string|URL} Root directory from which files would be served.
**Default:** `process.cwd()`.
* `port` {number}
* `host` {string} **Default:** `'localhost'`

* Returns: {http.Server}

Start a TCP server listening for connections on the given `port` and `host`, and
serve statically local files, using `directory` as the root. Please note that
when specifying a `host` other than `localhost`, you are exposing you local file
serve static local files, using `directory` as the root. Please note that
when specifying a `host` other than `localhost`, you are exposing your local file
system to all the machines that can connect to your computer.

If `port` is omitted or is 0, the operating system will assign an arbitrary
unused port, which will be output the standard output.
unused port, which it's output will be the standard output.

Also accessible via `require('node:http/server')`.

Expand All @@ -3596,7 +3596,8 @@ node -r node:http/server # starts serving the cwd on a random port
# To start serving on the port 8080 using /path/to/dir as the root:
node -r node:http/server /path/to/dir --port 8080
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a fan of building in a preload like this. Someone can easily use the -pe syntax to achieve this if they wanted, e.g. node -pe "require('http').createStaticServer(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One reason to use -r is you can get autocomplete when selecting the root directory, and it's easier to offer shorter versions of flags (i.e. I'd rather type node -r http/server . -p 8080 -h 0.0.0.0 than node -e 'http.createStaticServer({port: 8080, host: '0.0.0.0'})'). Currently this PR offers both syntax, so I guess I should also document that, and folks can chose the syntax they prefer, wdyt?


# Same, but exposing your local file system to the whole IPv4 network:
# Same as above, but exposing your local file system to the whole
# IPv4 network:
node -r node:http/server /path/to/dir --port 8080 --host 0.0.0.0
```

Expand Down