Artless HTTP server using .NET Core

The following article shows how to set up an artless HTTP server using .NET Core. I’ve already spoken about .NET Core and ASP.NET Core in the following posts:

 .NET Core 2.1 highlights: standing on the shoulders of giants

Developing APIs using Actor model in ASP.NET Core

Fast growing architectures with serverless and .NET Core

.NET Core is the brand new framework developed by Microsoft and the community. It follows an approach similar to NPM, furthermore, it is possible to install 3rd party global tools implemented by the community.  In practice, the .NET Core Global Tools are NuGet packages that contain a simple console application and they are runnable on your local machine. The following example shows how to use .NET Global tools to run an artless HTTP server.

Nate McMaster, a member of the ASP.NET Core team, has released a simple tool that provides an easy way to run an HTTP server on your machine. The only requirement is .NET Core 2.1 or newer installed on your development machine.

Run the HTTP Server

We can run the following command to install the tool:

dotnet tool install --global dotnet-serve

After that, it is possible to execute the command to run the HTTP server:

dotnet serve -o -S

The -o argument triggers the default browser, and the -S argument provides the HTTPS connection.

Also, the console application also accepts the following arguments:

dotnet serve --help
dotnet-serve 1.1.0-build.88+04e32cdef73c0e47b4fc74e35b5912b505cf2622
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.
-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 <EXT>=<MIME> Add a mapping from file extension to MIME type. Empty MIME removes a mapping.
-?|-h|--help Show help information

The command runs the server in the current path; if the folder contains an index.html file or, more in general, an file which can be served, it is returned by default. Otherwise, it will show the list of folders and data present in the path.

The following tool provides a quick and easy way to set up an HTTP server and provide content quickly. It also shows the power of .NET Core its portability. Since we are running it on .NET Core, it is cross-platform and runnable on every OS.

For more information, you can have a look at the following repository:

https://github.com/natemcmaster/dotnet-serve

Cover image – Winter lights Canary Wharf