Quick Start Guide
This guide will walk you through the most common use cases for serve
in just a few minutes.
Serving a Static Site
Let's start by serving a simple HTML file.
-
Create a project directory and a sample file.
First, create a new folder for your project and navigate into it:
mkdir my-static-site cd my-static-site
Next, create a basic
index.html
file:echo "<h1>Hello, world!</h1>" > index.html
-
Start the server.
Now, run
serve
in that directory. The easiest way is withnpx
:npx serve
-
View your site.
You will see output similar to this, indicating that the server is running:
┌──────────────────────────────────────────────────┐ │ │ │ Serving! │ │ │ │ - Local: http://localhost:3000 │ │ - On Your Network: http://192.168.1.100:3000 │ │ │ │ Copied local address to clipboard! │ │ │ └──────────────────────────────────────────────────┘
Open your web browser and navigate to
http://localhost:3000
. You should see your "Hello, world!" message. The local address is also automatically copied to your clipboard for convenience.
Serving a Specific Directory
If your files are in a subdirectory, or you want to serve a different directory than your current one, you can pass the path as an argument.
Suppose your project structure looks like this:
my-project/
├── dist/
│ └── index.html
└── package.json
To serve only the contents of the dist
folder, you would run:
serve dist
The server will now treat the dist
folder as the root directory.
Serving a Single-Page Application (SPA)
Single-page applications often use client-side routing, which requires all paths (like /about
or /contact
) to be handled by the index.html
file. serve
makes this easy with the --single
or -s
flag.
serve -s
With this command, any request that would normally result in a 404 Not Found
error will instead serve your index.html
file, allowing your client-side router to take over.
Stopping the Server
To stop the server, go back to your terminal and press Ctrl+C
.