What is serve?
The serve npm package is a static file serving and directory listing package that allows you to serve static files over HTTP quickly and easily. It is often used for hosting single-page applications, static websites, or for serving files during development.
What are serve's main functionalities?
Serving static files
This command starts a static file server in the specified directory. By default, it listens on port 5000, but you can specify another port with the -l flag.
npx serve /path/to/static/files
Directory listing
Running serve without any arguments serves the current directory and provides a directory listing if no index.html file is found.
npx serve
Single-page application mode
The -s flag enables single-page application mode, which rewrites all not-found requests to /index.html, allowing for client-side routing.
npx serve -s /path/to/spa
Custom port
This command starts the server on a custom port, in this case, port 4000.
npx serve -l 4000
SSL/TLS
Serve can also provide content over HTTPS by specifying the paths to the SSL certificate and key files.
npx serve --ssl-cert /path/to/cert.pem --ssl-key /path/to/key.pem
Other packages similar to serve
http-server
http-server is a simple, zero-configuration command-line HTTP server. It is powerful and has more configuration options than serve, such as the ability to cache files, set cache headers, and use proxies.
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Unlike serve, which is focused on static content, Express can handle dynamic content, middleware, routing, and much more.
lite-server
lite-server is a lightweight development server that serves a web app, opens it in the browser, and refreshes the browser on file changes. It is built on top of BrowserSync and is more suited for small projects and quick prototyping compared to serve.
serve
Simple 5 minute command-line file / directory server built with connect, inspired by nodejitsu's http-server to show off the simplicity and flexibility of connect as a modular server.
Installation
$ npm install -g serve
Usage
Usage: serve [options] [dir]
Options:
-v, --version output the version number
-F, --format <fmt> specify the log format string
-p, --port <port> specify the port [3000]
-H, --hidden enable hidden file serving
-S, --no-stylus disable stylus rendering
-J, --no-jade disable jade rendering
-I, --no-icons disable icons
-L, --no-logs disable request logging
-D, --no-dirs disable directory serving
-h, --help output usage information
Examples
HTTP Accept support built into connect.directory()
:
$ curl http://local:3000/ -H "Accept: text/plain"
bin
History.md
node_modules
package.json
Readme.md
Requesting a file:
$ curl http://local:3000/Readme.md
# serve
...
Requesting JSON for the directory listing:
$ curl http://local:3000/ -H "Accept: application/json"
["bin","History.md","node_modules","package.json","Readme.md"]
Directory listing served by connect's connect.directory()
middleware.
License
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.