A nushell library to create basic webservers.
Included features:
- open http server
- parse incomming requests
- route requests to nu functions
- send response
Missing (not planned) features:
- parse request headers and body
- windows support (
mkfifo
and netcat are required)
- Install all system-dependencies
- Download
webserver/mod.nu
aswebserver.nu
(also possible via nupm or numng) use
it in your nu scripts
System-dependencies:
This is a nu library. First you have to import it (see help use
)
http get http://localhost/index.html?foo=bar
will result in the following request
object:
╭──────────────┬───────────────╮
│ method │ GET │
│ path │ /index.html │
│ │ ╭─────┬─────╮ │
│ params │ │ foo │ bar │ │
│ │ ╰─────┴─────╯ │
│ http_version │ 1.1 │
╰──────────────┴───────────────╯
types:
method: string
path: string
params: list[record[string, string?]]
http_version: string
A response is a string or bytes containing a http server response.
There is a function to automate the creation:
Usage:
> format_http <status_code> <mime_type> <body>
Parameters:
status_code <int>: http status code (200 means ok)
mime_type <string>: example: text/plain
body <string>: the actual response
A list of mime-types can be found here.
Even shorter version:
- JSON:
application/json
- HTML:
text/html
In case your clients (like nushell http get
) for whatever reason think your json is binary data you can add
;charset=UTF-8
to the end of your mime-type (example: application/json;charset=UTF-8
) to tell them the encoding
directly.
Other response generators:
http_redirect <new_path>
A mapped server automatically maps requests to the responsible nu-function based on the path.
Example:
use webserver.nu *
# 8080 is the port the server will listen on
start_mapped_webserver 8080 {
"/time.json": {|request|
format_http 200 "application/json" (date now | to json)
}
"/hello.txt": {|request|
format_http 200 "text/plain" $"Hello ($request.params.name? | default World)!"
}
}
Suggestion: If you wan't to visualize / expose huge datasets with this the stor command offers a really good interface for this (its accessable from anywhere, fast, etc).
If you need more direct control you can use a basic webserver.
use webserver.nu *
# 8080 is the port the server will listen on
start_webserver 8080 {|request|
let path = ($request.path | str trim --left --char "/" | path expand)
if ($path | path type) == "file" {
# sending everything as text/plain will cause issues with images, etc, but this is just a basic example
format_http 200 "text/plain" (open -r $path)
} else {
format_http 404 "text/plain" "File not found"
}
}
If you want standard nushell error messages you can pass --crash-on-error
to start_webserver
or start_mapped_webserver
.
Another option is to pass --send-errors-to-client
, which will cause it to send errors to the http-client (browser / curl / ..).
- github-repo-backuper: This project archives github-repos into structured data and then uses
webserver.nu
to recreate a website.
If you have a opensource project using webserver.nu
feel free to open a PR or Issue to get it added.