Sunday, May 29, 2022

The simplest local 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.

On Mac computers may need to use `python3` command instead of  `python`

To run server of different port, just add port number to the command, i.e. for port 9000

> python3 -m http.server 9000 

When using node.js / npm, there is also a web server module, that can be installed "globally"

> npm install http-server -g
and then to can use it in any folder/directory

> 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 your own, 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: