Sunday, May 29, 2022

simplest web file server

How do you set up a local testing server? - Learn web development | MDN

with Python (3.x) installed on the computer, it is just one command:

> python -m http.server

By default, this will run the contents of the directory on a local web server, on port 8000. You can go to this server by going to the URL localhost:8000 in your web browser. Here you'll see the contents of the directory listed — click the HTML file you want to run.

If using node.js / npm, there is also a module for that

> npm install http-server -g

> cd D:\Folder
> http-server
> http-server D:\Folder
> http-server -a localhost -p 8080
http-party/http-server: a simple zero-configuration command-line http server @GitHub

and to make self-sufficient (no dependencies) exe, GoLang may be the simplest way to go :)

Serve is a very simple static file server in go
  -p="8100": port to serve on
  -d=".":    the directory of static files to host
Navigating to http://localhost:8100 will display the index.html or directory
listing file.
package main
import (
func main() {
  port := flag.String("p", "8100", "port to serve on")
  directory := flag.String("d", ".", "the directory of static file to host")
  http.Handle("/", http.FileServer(http.Dir(*directory)))
  log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
  log.Fatal(http.ListenAndServe(":" + *port, nil))

of course, then you need Go SDK installed, and need to build with

> go build web_srv.go

then run, with no dependencies on any compatible machine (same OS and CPU type)
> web_srv

to reduce exe size from about 6 MB to about 4MB, remove debug info when compiling
> go build -ldflags "-w" web_srv.go

No comments: