Skip to main content

Quickly Serving Local Content

· 2 min read

Sometimes you need to serve some local static content over HTTP/HTTPS for testing SPAs or other things.

I recently stumbled across the dotnet-serve tool which is a dotnet cli global tool. It provides similar basic functionality to the Python module, however it also allows you to do a couple of extra things like returning headers, enabling HTTPS, and enabling content compression.

I've used the python SimpleHTTPServer module

python -m SimpleHTTPServer 8000

However, if you're on Windows or want to enable SSL, then this Python module isn't the ideal solution.

Dotnet Serve

If you're a dotnet developer, this is handy because it just adds another tool to the default dotnet cli command.

Installation

dotnet tool install --global dotnet-serve

dotnet serve --help
dotnet-serve 1.7.137+e153893585e7ccb707ba0d532b54288c49e59bb8

A simple command-line HTTP server

Usage: dotnet serve [options]

Options:
--version Show version information
-d|--directory <DIR> The root directory to serve. [Current directory]
-o|--open-browser Open a web browser when the server starts. [false]
-p|--port <PORT> Port to use [8080]. Use 0 for a dynamic port.
-a|--address <ADDRESS> Address to use [127.0.0.1]
--path-base <PATH> The base URL path of postpended to the site url.
--default-extensions[:<EXTENSIONS>] A comma-delimited list of extensions to use when no
extension is provided in the URL. [.html,.htm]
-q|--quiet Show less console output.
-v|--verbose Show more console output.
-h|--headers <HEADER_AND_VALUE> A header to return with all file/directory
responses. e.g. -h "X-XSS-Protection: 1; mode=block"
-S|--tls Enable TLS (HTTPS)
--cert A PEM encoded certificate file to use for HTTPS
connections.
Defaults to file in current directory named
'cert.pem'
--key A PEM encoded private key to use for HTTPS
connections.
Defaults to file in current directory named
'private.key'
--pfx A PKCS#12 certificate file to use for HTTPS
connections.
Defaults to file in current directory named
'cert.pfx'
--pfx-pwd The password to open the certificate file.
(Optional)
-m|--mime <MAPPING> Add a mapping from file extension to MIME type.
Empty MIME removes a mapping.
Expected format is <EXT>=<MIME>.
-z|--gzip Enable gzip compression
-b|--brotli Enable brotli compression
-c|--cors Enable CORS (It will enable CORS for all origin and
all methods)
--save-options Save specified options to .netconfig for subsequent
runs.
--config-file Use the given .netconfig file.
-?|--help Show help information

Usage

Starting server, serving .
Listening on:
http://localhost:52244

Press CTRL+C to exit

Basic Usage

dotnet serve -o

HTTPS Usage

dotnet serve -o -S