• Stars
    star
    663
  • Rank 67,991 (Top 2 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple command-line HTTPS server for the .NET Core CLI

dotnet-serve

Build Status Code Coverage NuGet NuGet Downloads

A simple command-line HTTP server.

It launches a server in the current working directory and serves all files in it.

Get started

Install .NET 6 or newer and run this command:

dotnet tool install --global dotnet-serve

Start a simple server and open the browser by running

dotnet serve -o

..and with HTTPS.

dotnet serve -o -S

... with a specific port (otherwise, it defaults to a random, unused port).

dotnet serve --port 8080

... with access allowed to remote machines (defaults to loopback only.) Use this if running inside Docker.

dotnet serve --address any

Usage

Usage: dotnet serve [options]

Options:
  --version                            Show version information.
  -d|--directory <DIR>                 The root directory to serve. [Current directory]
  -o|--open-browser[:<PATH>]           Open a web browser when the server starts. [Default false]
                                       You can also provide the subpath to launch by using -o:<PATH>.
                                       Example: -o:/path/index.html
  -p|--port <PORT>                     Port to use [8080]. Use 0 for a dynamic port.
  -a|--address <ADDRESS>               Address to use. [Default = localhost].
                                       Accepts IP addresses,
                                       'localhost' for only accept requests from loopback connections, or
                                       'any' to accept requests from any IP address.
  --path-base <PATH>                   The base URL path of postpended to the site url.
  --reverse-proxy <MAPPING>            Map a path pattern to another url.
                                       Expected format is <SOURCE_PATH_PATTERN>=<DESTINATION_URL_PREFIX>.
                                       SOURCE_PATH_PATTERN uses ASP.NET routing syntax. Use {**all} to match anything.
  --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.
  --fallback-file                       The path to a file which is served for requests that do not match known file names.
                                       This is commonly used for single-page web applications.
  -?|--help                            Show help information.

Tip: single letters for options can be combined. Example: dotnet serve -Sozq

Configuring HTTPS

dotnet serve -S will serve requests over HTTPS. By default, it will attempt to find an appropriate certificate on the machine.

By default, dotnet serve will look for, in order:

  • A pair of files named cert.pem and private.key in the current directory
  • A file named cert.pfx in the current directory
  • The ASP.NET Core Developer Certificate (localhost only)

You can also manually specify certificates as command line options (see below):

See also this doc for how to create a self-signed HTTPS certificate.

.pem files

Use this when you have your certficate and private key stored in separate files (PEM encoded).

dotnet serve --cert ./cert.pem --key ./private.pem

Note: currently only RSA private keys are supported.

.pfx file

You can generate a self-signed

Use this when you have your certficate as a .pfx/.p12 file (PKCS#12 format).

dotnet serve --pfx myCert.pfx --pfx-pwd certPass123

Using the ASP.NET Core Developer Certificate

The developer certificate is automatically created the first time you use dotnet. When serving on 'localhost', dotnet-serve will discover and use when you run:

dotnet serve -S

Reverse Proxy

dotnet-serve --reverse-proxy /api/{**all}=http://localhost:5000 will proxy all requests matching /api/* to http://localhost:5000/api/*.

The source path pattern uses ASP.NET routing syntax. See the ASP.NET docs for more info.

Multiple --reverse-proxy <MAPPING> directives can be defined.

Reusing options with .netconfig

dotnet-serve supports reading and saving options using dotnet-config, which provides hierarchical inherited configuration for any .NET tool. This means you can save your frequently used options to .netconfig so you don't need to specify them every time and for every folder you serve across your machine.

To save the options used in a particular run to the current directory's .netconfig, just append --save-options:

dotnet serve -p 8080 --gzip --cors --quiet --save-options

After running that command, a new .netconfig will be created (if there isn't one already there) with the following section for dotnet-serve:

[serve]
	port = 8000
	quiet
	gzip
	cors
	header = X-My-Option: foo
	header = X-Another: bar

(note multiple header, mime type mappings and exclude-file entries can be provided as individual variables)

You can place those settings in any parent folder and it will be reused across all descendent folders, or they can also be saved to the global (user profile) or system locations. To easily configure these options at those levels, use the dotnet-config tool itself:

dotnet config --global --set serve.port 8000

This will default the port to 8000 whenever a port is not specified in the command line. You can open the saved .netconfig at %USERPROFILE%\.netconfig or ~/.netconfig.

The cert, key and pfx values, in particular, can be relative paths that are resolved relative to the location of the declaring .netconfig file, which can be very convenient.

More Repositories

1

CommandLineUtils

Command line parsing and utilities for .NET
C#
2,092
star
2

dotnet-tools

A list of tools to extend the .NET Core command line (dotnet)
1,514
star
3

DotNetCorePlugins

.NET Core library for dynamically loading code
C#
1,435
star
4

LettuceEncrypt

Free, automatic HTTPS certificate generation for ASP.NET Core web apps
C#
1,396
star
5

Yarn.MSBuild

MSBuild integration for the Yarn package manager.
C#
70
star
6

msbuild-tasks

Sample MSBuild tasks for .NET Core and NuGet. (Issues: https://github.com/natemcmaster/blog)
C#
65
star
7

aspnetcore-webpack-hmr-demo

Demo of ASP.NET Core + webpack + hmr, and a few other things (Open issues on https://github.com/natemcmaster/blog)
C#
57
star
8

dotnet-globaltool-templates

Template for a .NET Core global tool project (please open issues in https://github.com/natemcmaster/blog)
PowerShell
24
star
9

xunit-extensions

Making XUnit.NET test projects even better
C#
23
star
10

xunit-cli

A global .NET Core command line tool for running XUnit tests
C#
9
star
11

EntityFrameworkCore.Samples

C#
7
star
12

ef-docker-talk

This repo contains the slides, demos, and code used for a talk I gave in 2016 about Docker, .NET Core, and Entity Framework Core
C#
7
star
13

dnvm

The .NET Core version manager (prototype, no longer under development). Use https://github.com/natemcmaster/blog for issues
C#
4
star
14

catan

Settlers of Catan for BYU CS 340
JavaScript
3
star
15

NuGetNetBuildTools

C#
3
star
16

srihash-cli

A command-line utility for generating the SRI hash
C#
2
star
17

msbuild-demos

Small demos of how MSBuild works
C#
2
star
18

blog

Yet-another Jekyll blog
HTML
2
star
19

alfred-sublime-text-workspace

See all recent Sublime Text workspaces in Alfred.
Python
2
star
20

dotnet-command

Extension for .NET Core CLI to run commands on a project
C#
2
star
21

.github

Default special GitHub config files
1
star
22

nuget-cli

C#
1
star
23

bbq-js

A light JS framework that imitates Angular.
JavaScript
1
star
24

libsqlite3-package

sqlite3 build automation and packaging. [Archived - see README]
Shell
1
star
25

catan_source

Settlers of Catan, view layer, BYU CS 340 Winter 2014
JavaScript
1
star
26

historyexplorer

An interactive webpage for exploring Western history from the Renaissance to present day
JavaScript
1
star