LinuxCommandLibrary

serve

Serve static files over HTTP

TLDR

Start an HTTP server listening on the default port to serve the current directory

$ serve
copy

Start an HTTP server on a specific [p]ort to serve a specific directory
$ serve -p [port] [path/to/directory]
copy

Start an HTTP server with CORS enabled by including the Access-Control-Allow-Origin: * header in all responses
$ serve [[-C|--cors]]
copy

Start an HTTP server on the default port rewriting all not-found requests to the index.html file
$ serve [[-s|--single]]
copy

Start an HTTPS server on the default port using the specified certificate
$ serve --ssl-cert [path/to/cert.pem] --ssl-key [path/to/key.pem]
copy

Start an HTTP server on the default port using a specific configuration file
$ serve [[-c|--config]] [path/to/serve.json]
copy

Display help
$ serve --help
copy

SYNOPSIS

serve [-p port] [-s] [-c cache_time] [-g gzip] [-C] [-S] [-n] [-u]

PARAMETERS

-p, --port [port]
    Specify the port to listen on. Defaults to 3000.

-s, --single
    Serve a single page application (SPA).

-c, --cache [time]
    Set cache time (in seconds). Defaults to 3600.

-g, --gzip [flag]
    Enable or disable gzip compression. Default is to enable gzip.

-C, --cors
    Enable CORS with default configuration.

-S, --no-slash-redirect
    Disable redirecting folders to the corresponding slash path.

-n, --no-clipboard
    Do not copy the address to the clipboard.

-u, --username [username]
    Add basic authentication.

--password [password]
    Password for basic authentication. Use with username option.

DESCRIPTION

The `serve` command is a simple, zero-configuration static HTTP server. It's designed for quickly serving files from the current directory, making it ideal for testing web pages or sharing files locally. Unlike more complex web servers like Apache or Nginx, `serve` focuses on ease of use and minimal setup.

It is typically installed via npm, the Node Package Manager and is an excellent choice when you need a quick and simple web server to distribute your files.

CAVEATS

Because `serve` is very simplistic, it lacks advanced features like SSL/TLS encryption, virtual hosts, and complex routing rules. It's primarily suitable for development or serving static content within a trusted network. When serving content on a public network, consider using a more robust web server.

INSTALLATION

The `serve` command is typically installed globally using npm: `npm install -g serve`.

USAGE EXAMPLE

To serve the current directory on port 8080, run: `serve -p 8080`.

SINGLE PAGE APPLICATION

Serving a SPA: `serve -s` or `serve --single`.

HISTORY

The `serve` command is a modern utility, commonly used in Node.js environments. Its rise in popularity is directly linked to the increasing reliance on JavaScript-based web development workflows, where a quick and easy local server is crucial for testing and development.

SEE ALSO

python3 -m http.server(1), nginx(8), apache2(8)

Copied to clipboard